Does a 2-dimensional array use as 1-dimensional array? Could cause Undefined behavior or so?

Is this code correct? Is using a 2-dimensional array as one-dimensional, obsolete for some reason?

char tab1[3][3]; for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) tab1[i][j] = (char)i; printf("%c", ((char*)tab1)[3]); // == tab1[1][0] 
+5
source share
2 answers

This is normal, if not for any other reason, because char can use all other types in accordance with the standard. Multidimensional arrays are also allocated as an adjacent block, so for starters it is a one-dimensional array in memory.

We know that tab1 splits into a pointer to the first element of an adjacent multidimensional array (which is an array) and distinguishes this array pointer to char* (which can be any alias) should be good.

When in doubt, you can always do &tab1[0][0] . Again, since we know that allocated memory is contiguous.

EDIT

If it was an integer array ( int tab1[3][3] ), then the story is slightly different. As far as I understand before C++17 , the standard says that a pointer to the correct type of an object is valid regardless of how it got its value. This means that casting to int* must be accurate, because we know that the address received by tab1 matches the address of the first element.

However, after C++17 this guarantee was removed, so this behavior is likely to be undefined.

See THIS question for more details .

For this reason, I always recommend using &tab1[0][0] , which will always be valid.

0
source

Uses a 2-dimensional array as one-dimensional, deprecated for some reason?

When you use tab1 way you have it, it splits into a pointer of type char (*)[3] (a pointer to an array of 3 char s). It does not break into char* .

You cannot use it as a char* without casting it explicitly.

Re: Is this code correct?

It is well defined because for tab1 in your code, &tab1 , &tab1[0] and &tab1[0][0] point to the same place in memory, even if they are all different.

0
source

Source: https://habr.com/ru/post/1274537/


All Articles