A question about Arrays

Can anybody tell me what is going on here. Why is my array not retaining the changes?

const byte PTposition[11] = {0x90, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void setup() {
  Serial.begin(115200);

 Serial.println(PTposition[0]);
 Serial.println(PTposition[1]);
 Serial.println(PTposition[2]);   //This returns 0 which is correct
const byte PTposition[11] = {0x90, 0x50, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //array[3] changed to 0x06 
Serial.println(PTposition[0]);
 Serial.println(PTposition[1]);
 Serial.println(PTposition[2]);  //This shows the change correctly as 6
}

void loop() {
 Serial.println(PTposition[0]);
 Serial.println(PTposition[1]);
 Serial.println(PTposition[2]);  //This now returns 0 again????? why???????????
delay(5000);

}

Retain what changes? Please give us a complete problem description.

Two arrays with the same name.

Take a look at C/C++ scope rules.

1 Like

this is defining a new array within setup();

a variable/array can only be initialized with values `PTposition[11] = {`` using this syntax.

So what we are saying is that an array is only valid from within the function it was created and cannot be used by called upon by another function am I correct?

No, you are almost there.
The first array has global scope, so available in any function.
The second array in setup is visible only in setup, after its definition.

So it the global array can be called by any function but not re-defined outside of the function that created it. So I cannot edit a global array from within a function only create a copy with changes which will only apply within that function. Bummer!

You can't edit a "const" array, period.

2 Likes

ok so if I take off the const is there away to edit the global array from a function?

if it's global, it's not defined within the scope of a function

no, any global variable can be modified by any function

consider

byte PTposition[11] = {0x90, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void setup() {
    Serial.begin(115200);

    Serial.println(PTposition[0]);
    Serial.println(PTposition[1]);
    Serial.println(PTposition[2]);   //This returns 0 which is correct
    PTposition [0] = 0x12;
    Serial.println(PTposition[0]);
    Serial.println(PTposition[1]);
    Serial.println(PTposition[2]);  //This shows the change correctly as 6
}


void loop() {
    Serial.println(PTposition[0]);
    Serial.println(PTposition[1]);
    Serial.println(PTposition[2]);  //This now returns 0 again????? why???????????
    delay(5000);

}

output

144
80
0
18
80
0
18
80
0
18
80
0

At your skill level, the best way is to just keep the array global and access it directly from the function. It's when the function should modify multiple arrays that you need to do more complex things.

If you can't do things, the compiler will error.

Your code didn't attempt to redefine the global array, it only made a new local array of the same name.

You can edit the contents of a global array, but not with an initialization list, and also not when it is declared with a const modifier.

Ok thank you all for the help. I can change the Gobal array but not by re-defining it this way I was. I have to edit specific elements.

You can use memcpy too.

You probably mean, "initialize" specific elements. And, you probably mean that you have to re-initialize some values. Instead of relying on boot time initialization of the desired values, you should write code to do it.

It's a program design issue, unrelated to the scope issue that you stumbled on.

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