Trying to redeclare an array with different....

number of elements and content from the original declared.

//Hello I am trying to redeclare a varible array (bytes),
#define countof(a) (sizeof(a) / sizeof(a[0]))
byte SetDeadTimeIC[16]; //Well I am trying to create some kind of empty "buffer" first, which later I will
//that will have slightly more elements than I will ever use in the future code.My idea was to reserve more
//memory space than required and then when I redeclare the array future in my code (with different number of elements and different
//content) 
void setup()
{
   Serial.begin(9600);
    myloop();
}




void loop()
{
  //
 
}


void myloop()
{
  byte a,b; //Those varibles are dependant on a rotary encoder, so when I rotate the mechanical encoder "a" and "b" will change
  //Those variables are in 0x format
  if((a==0x2B) && (b==0x2B))
  {
byte SetDeadTimeIC[]={0x2B,0xE3,0x00,0x36,a,0x2B,0x00,b,0x2B}; 
  }
 if((a==0x2B) && (b!=0x2B)) 
  {
 byte SetDeadTimeIC[]={0x2B,0xE3,0x00,0x36,a,0x2B,0x00,b};    
  }
if((a!=0x2B) && (b!=0x2B))
  {
 byte SetDeadTimeIC[]={0x2B,0xE3,0x00,0x36,a,0x00,b}; 
  }
  //As you can see from the code above  I am trying to "insert" an extra element after right after "a" or "b" or both
  //dependant on that if "a","b" or both are equal to 0x2B
  for(int ee=0;ee<countof(SetDeadTimeIC);ee++)
  {
    Serial.print(SetDeadTimeIC[ee],BYTE); 
  }
  
  
}

We'll if you can show a specific solution to this problem or somekind of workaround I will be very grateful :-/

You can't make sizeof(SetDeadTimeIC) change after the initial declaration, but you could hold the intended size in a separate variable.

[glow]byte[/glow] SetDeadTimeIC[]={0x2B,0xE3,0x00,0x36,a,0x2B,0x00,b,0x2B};

"byte" here means you try to declare a new variable that will die at the following "}". Make assignments to individual array positions instead, e.g.

SetDeadTimeIC[4] = a;
SetDeadTimeIC[5] = 0x2B;
...

Once you declare a variable, whether a scalar or an array, it has a fixed type and size. You can not re-declare it with a different type or size.

It does not appear that there is any reason for changing the size of array, in your example. You simply want to store different data in the array, which you can do. You simply need to store the new data one element at a time.

The countof macro that you defined will work for global arrays or for local arrays. It will not work for function arguments. That is, if myloop() took an argument:

void myloop(byte deadTime[], byte a, byte b)
{
}

and was called:

byte a = 12;
byte b = 56;
byte myArray[15];
myloop(myArray, a, b);

Inside myloop, the countof function would NOT return the size of the array passed in.

Finally, as a and b are local variables in myloop, they will always equal 0.

//Those variables are in 0x format
if((a==0x2B)

This comment does not make sense. The way the value is stored internally by the Arduino has nothing to do with how you compare the value to constants. The value 0x2B is 43 in decimal and 00101100 in binary and 54 in octal. You can use whichever constant representation makes the most sense. Counting in hex is for computers. Humans (at least most of us) don't have 16 fingers, so counting in decimal makes more sense. Since a and b represent rotary encoder counts, it makes sense to represent the constants in a human-friendly format. Not that it makes any difference to the compiler.

Well I fixed my problem but I still wanna know is there some way to redeclare an array in the way I tried to ask before.
Btw I couldn't undestand what you were showing. :-?
I still a great noob

//Hello I am trying to redeclare a varible array (bytes),
#define countof(a) (sizeof(a) / sizeof(a[0]))
//byte SetDeadTimeIC[16];I DON'T USE IT ANYMORE //Well I am trying to create some kind of empty "buffer" first, which later I will
//that will have slightly more elements than I will ever use in the future code.My idea was to reserve more
//memory space than required and then when I redeclare the array future in my code (with different number of elements and different
//content) 
void setup()
{
   Serial.begin(9600);
    myloop();
}




void loop()
{
  //
 
}


void myloop()
{
  byte a,b; //Those varibles are dependant on a rotary encoder, so when I rotate the mechanical encoder "a" and "b" will change
  //Those variables are in 0x format,those are not exactly counts of encoder rotation,they are variables that are affected by the 
  //counts 
 
  for(int ee=0;ee<countof(SetDeadTimeIC);ee++)
  {
    Serial.print(SetDeadTimeIC[ee],BYTE);
    if(ee!=0 && SetDeadTimeIC[ee]==0x2B)
    {
      Serial.print(0x2B,BYTE);
    }
  }
  
  
}

I still wanna know is there some way to redeclare an array

No. There isn't. Once an array (or scalar variable) is declared, it exists, with the specified type and size, until it goes out of scope.

I wonder if this will help....

If you declare an array of, say, 16 elements, it will then always HAVE 16 elements... but you don't need to USE all of them! You can change what is stored in the array.... and you don't have to store 16 elements each time you store SOME elements in the array. But, if you delared it as having 16 elements at the outset, you can't, later, store MORE THAN 16 elements.

1 Like