Const Multidimensional array

10,932

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.

Share:
10,932

Related videos on Youtube

Mithun
Author by

Mithun

Updated on September 26, 2022

Comments

  • Mithun
    Mithun over 1 year

    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;