Const Multidimensional Array

In C, how would you add a const modifier (or any other modifier) ​​to a global multidimensional array so that both the variable and the values ​​it has are constant.

For example, how to add a const modifier to it:

byte fruitIds[][2] = { { 0x01, 0x02}, {0x02, 0x03} } 

so that at the end of the assignment you cannot do this:

 fruitIds = vegetableIds; 

or that:

 fruitIds[0] = {0x02, 0x03}; 

or that:

 fruitIds[0][0] = 0x02; 
+4
source share
1 answer

Arrays are no longer modified by lvalues. It just means you need to make const values:

 const byte fruitIds[][2] = { { 0x01, 0x02}, { 0x02, 0x03} }; 

These assignments from your message:

 fruitIds = vegetableIds; fruitIds[0] = {0x02, 0x03}; 

Already illegal. The latter is not even the correct syntax, but I get a read-only variable is not assignable from clang trying to execute the first.

+3
source

All Articles