Initialize Char Array & Pointer Arithmetic

I wrote a function to convert a decimal number to binary, and it seems to be working.

But the expression: *charBinary++ = ‘0’;
in the INIT ARRAY for loop throws a compile error: “lvalue required as increment operand”.

Because operator precedence dictates that postfix operators (such as increment) have higher precedence than prefix operators (such as dereference), I would have thought that my expression *charBinary++ is equivalent to *(charBinary++).

My aim with the above expression is to increase the value of “charBinary” (so it now points to the next element), but because I have ++ as postfix, the whole expression should be evaluated as the value pointed originally by the pointer.

void setup()
{
   // INIT ARRAY
   byte b;
   byte array_size = 33;
   char charBinary[array_size];
   for (b = 0; b < array_size - 1; b++)
   {
      *charBinary++ = '0'; // COMPILE ERROR
      // *(charBinary + b) = '0'; WORKS AS EXPECTED
   }
   charBinary[array_size - 1] = '\0';

   // DISPLAY STRING
   Serial.println();
   Serial.printf("charBinary[%d]: ", GetStringLength(charBinary));
   for(b = 0; b < array_size; b++)
   {
      Serial.print(*(charBinary + b));
   }

   // CONVERT DEC TO BINARY
   int i = 23;
   DecimalToBinary(i, charBinary, array_size - 1);

   // PRINT RESULT
   Serial.println();
   Serial.printf("Decimal: %d Binary: %s", i, charBinary);
   Serial.println();
}

void DecimalToBinary(int decNum, char* strBin, byte len)
{
   for (int c = len - 1; c >= 0; c--)
   {
      *strBin++ = ((decNum >> c) & 1) ? '1' : '0';
   }
}

When I change the INIT code in the for loop to use a char pointer: *p++ = ‘0’; it works!

void setup()
{
  byte b;
  byte array_size = 33;
  char charBinary[array_size];
  char *p = charBinary;
  for (b = 0; b < array_size - 1; b++)
  {
     *p++ = '0';
  }
  charBinary[array_size - 1] = '\0';
}

Why does this line in INIT ARRAY fail when using the array name, while the code above using a char pointer works?
*charBinary++ = ‘0’;

I thought arrays and pointers are synonymous in terms of how they’re used to access memory, and that an array name is a const pointer to the array (ie. const int*).

an array variable is not always same as a pointer

So you created an array called charBinary. Then you attempt to make that pointer point somewhere else. How can you find the array again after you do that? You have lost it.

The compiler is trying to prevent that loss. It is more like the charBinary pointer is a constant that should never change.

Thanks a lot for the explanation MorganS. That makes a lot of sense. Now I understand why this is valid: *(charBinary + b) = '0';

but this expression is not: *charBinary++ = '0';

because as you say, if we could increment or decrement the array pointer, we would lose track of its base address.

ilovetofu: Thanks a lot for the explanation MorganS. That makes a lot of sense. Now I understand why this is valid: *(charBinary + b) = '0';

but this expression is not: *charBinary++ = '0';

because as you say, if we could increment or decrement the array pointer, we would lose track of its base address.

I don't thing the compiler cares about this. Google the difference between array and pointer