Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const Multidimensional array

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

For example how would I add a const modifier to this:

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

so that at the end of the assignment you can't do this:

fruitIds = vegetableIds;

or this:

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

or this:

fruitIds[0][0] = 0x02;
like image 656
Mithun Avatar asked Jan 31 '13 00:01

Mithun


1 Answers

Arrays are already non-modifiable lvalues. That just means you need to make the values const:

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

These assignments from your post:

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

Are already illegal. The latter isn't even valid syntax, but I get a read-only variable is not assignable message from clang trying to do the former.

like image 87
Carl Norum Avatar answered Sep 21 '22 14:09

Carl Norum