Difference between array and pointer to array after compilation?

void m() { char a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; char(*c)[3][3] = (char (*)[3][3])a; printf("%d\n", *c[0][0]); } 

For example, in this function, the variable a indicates a place in memory with 9 integers per line.

But what about c ? Does c indicate a memory location that points to a memory location containing 9 integers in a string?

So, technically, c pointer to one layer or a pointer to two layers?

Can't you say what I said above? When I execute the following function:

 void m() { char a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; char(*c)[3][3] = (char (*)[3][3])a; printf("%d\n", *c[0][0]); printf("a...%p\nc...%p\n", a, c); } 

a and c both point to the same locations? Should it not be a two-layer pointer, and a should be a pointer to a location in memory?

+1
source share
2 answers

the variable a indicates a place in memory with 9 integers per line.

No. The variable a is a place in memory with 9 integers per line. a is just a name for this location.

Does c indicate a memory location that points to a memory location that contains 9 integers in a string?

No. c is a place in memory that points to a place in memory that contains 9 integers per line. c is the name for the location containing the pointer.

So, technically, is there one pointer to one layer or a pointer to two layers?

Single

+2
source

Take a look at these type definitions:

 typedef char9[9]; typedef char3x3[3][3]; 

If we check the sizes:

 cout<<sizeof(char9); 
Result

there will always be 9, there are no alignments in the char array.

 cout<<sizeof(char3x3); 

if it is 9, it is safe to say that bytes are ordered in the same way as one dimensional array. if it is greater than 9, I would say that there is alignment between the lines, so there are holes between the lines, and then you cannot map a pointer of one type to another.

0
source

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


All Articles