简体   繁体   中英

C Array of const unsigned char[][]

I'm a little turned around on the exact syntax for doing this. I have one array defined as such:

const unsigned char ARR[SIZE][SIZE] = {...}

and I want to have it in an array so I can do something to the effect of

ARR2[0] = ARR

I've tried const unsigned char ARR2[][SIZE][SIZE] = {ARR} and const unsigned char* ARR2[SIZE][SIZE] = {ARR} , but neither of those worked. Can someone point out the correct syntax for having a constant array of constant two dimensional arrays of unsigned characters?

From your comment to haccks' answer, it sounds like you want this:

const unsigned char ARR[SIZE][SIZE] = {...};
const unsigned char (*ARR2[])[SIZE][SIZE] = {&ARR};

If you want ARR2 itself to be const , that would be like this:

const unsigned char (* const ARR2[])[SIZE][SIZE] = {&ARR};

It should perhaps also be noted that you cannot use this abomination to access ARR[x][y] as ARR2[0][x][y] , but need to do (*ARR2[0])[x][y] (or, equivalently, ARR2[0][0][x][y] ).

Another corollary that might be noteworthy is that you cannot assign ARR2 with ARR2[0] = ARR , but you need explicitly to do ARR2[0] = &ARR (at least to avoid warnings). The reason is that ARR degenerates to a pointer to its first element , that is, a const unsigned char * whose value is &ARR[0][0] , whereas ARR2[0] expects to be assigned a const unsigned char (*)[SIZE][SIZE] , which is obtained with &ARR . While the pointer value is identical in both cases, the types aren't.

Seems like what you are trying to achieve is impossible since left operand of assignment operator can be only of arithmetic, structure / union or pointer type, but not an array one. See here for details: 6.5.16.1 Simple assignment .

So the only way for you is only memcpy() 'ing.

Try this

const unsigned char (* ARR2)[SIZE] = ARR; 

You mentioned in your comment that you want ARR2 to be an array that can hold array ARR as it's first element but, this is not possible . ARR is an array and it will converted to pointer to it's first element expect when it's an operand of unary & and sizeof operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM