Pointer 101 help?

uint16_t a[4];
uint16_t *p_a;

p_a = a;

I can do this and have all day in CCS with gnu under the hood and it works, yet if I try to do it in workbench IDE it errors out saying

‘p_a’ does not name a type

What gives?

This is different from what you show in the other thread where you have:

int *p_a;
int a[4];

p_a = &a[0];

Is this intentional as you made changes, or a typo in one?

May be different as I am scrambling trying things at this end…No reason this shouldn’t work…Works fine in CodeComposer which uses gnu tools…This is so fundamental it has me thinking the compiler I am using is strange. I do notice when I hover over it

This declaration has no storage class or type specifier C/C++(77)

Is there some way to look at the properties of the compiler in Particle WorkBench…Was thinking it is bcz I am not using C99??

Both versions are (roughly) synonymous and both should work and do for me in my tests.

@ridgerunnersjw, what device OS version are you targeting?
I’m pretty sure Workbench uses C++14 for Device OS 1.2.1 and later.

Maybe you should test such hypotheses in a stripped down test project to exclude any other factors that may contribute and which you can share in its entirety too.

As commented in this thread

1 Like

I don’t have any trouble compiling that code:

#include "Particle.h"


void setup() {
    uint16_t a[4];
    uint16_t *p_a;

    p_a = a;
}

void loop() {

}

Tested with a few Device OS versions, all worked properly:

particle compile argon . --target 3.2.0 --saveTo firmware.bin
particle compile argon . --target 2.3.0 --saveTo firmware.bin
particle compile argon . --target 1.4.4 --saveTo firmware.bin

I am using Argon 2.3.0 and I can tell you that the following compiles:

char Array[4];
char *p_Array = Array;

and the following does NOT:

char Array[4];
char *p_Array; 
*p_steve = Array;

yielding the error:
error: expected constructor, destructor, or type conversion before ‘=’ token
18 | *p_steve = Array;

In short it has something to do with your compiler not allowing anything but initializations to globals. Having declaration and definition in single step seems to show that. Isn’t there a way to change compiler settings? Why wouldn’t you allow the user options to change compiler? (I understand globals are frowned on in the software world but as a hardware guy it is easy to do for testing purposes especially when your coding skills are up to software standards) …

Is p_steve supposed to be p_Array? If not, what is p_steve declared as?

If it is p_Array then it should be:

p_Array = Array;

You want to set the pointer value of p_Array to the array. With the * on the left hand side you’re setting the dereferenced location of an uninitialized pointer, and the right hand side would need to be a char not a char [].

3 Likes

My apologies…yes p_steve is supposed to be p_Array…and like I’ve said before that doesn’t work either…this fails compiling…same error… it ONLY likes when the declaration and the assignment of the dereferenced (*) pointer is set to a value…

As I mentioned these are all global variables…does it have something to do with that??

This does not work

char Array[4];
char *p_Array; 
p_Array = Array;

nor does this:

char Array[4];
char *p_Array; 
p_Array = &Array[0];

All the same error see above

Try creating a new project and compiling this code. It works for me:

#include "Particle.h"


void setup() {
    char Array[4];
    char *p_Array; 
    p_Array = Array;
}

void loop() {
}

Oh, I think I know what’s happening. Do you have the definitions as globals, like this:

#include "Particle.h"


char Array[4];
char *p_Array; 
p_Array = Array; // Not valid, can't be here

void setup() {
}

void loop() {

}

You can’t do that because you can’t make a global assignment except during initialization outside of a function.

This works, because the assignment is in a function:

#include "Particle.h"


char Array[4];
char *p_Array; 

void setup() {
    p_Array = Array;
}

void loop() {

}

Also, this, which is during initialization:

#include "Particle.h"


char Array[4];
char *p_Array = Array; 

void setup() {
}

void loop() {

}

That does work! I get the warning that p_Array was not used but in the end it does compile…Now one difference I see is you have it in setup…yes I now see your last post…

I’m a hardware guy so bear with me here…but
isn’t the second part of your post above not really the correct way to assign a pointer?..
meaning isn’t it BEST to have the pointer point to the address of some variable (and also the safest thing to do) rather than
assigning the pointer value to the value of Array[0]…

I was always under the impression that you should always “anchor” the pointer to an address when defining it where

char *p_Array = Array;

this “feels” like you are not "anchoring the address but rather assigning (derefencing) the value of the pointer to Array[0] not its address…Please correct me here as I may be misundertanding this
Thank you

char *p_Array = Array;

This assigns a pointer to Array, not a duplicate of the contents. The reason is the left-hand side is a pointer (char *) and the right-hand side is an array. Even though you don’t have an explicit pointer (&Array) that is not necessary because the compiler always implicitly treats an array as a pointer and passes it by reference, not by value.

Thank you…

I believe I have my problem solved :slightly_smiling_face:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.