0

I was learning about pointers and multidimensional arrays.

I know that they are comprised of several sub arrays arranged one after the other like this.

int C[ 3 ][ 2 ][ 2 ];

enter image description here

In this example C = 800 This is a pointer to the first array element of the 3 integer array.

So, I assume there is a block of memory reserved to store the reference variable C like this enter image description here

What I don't understand is how dereferencing work with arrays.

In normal context *C mean get the value stored at the address 800. So *C = 2

But here *C would be 800. I understand this logically. This is a pointer to the 2 integer array inside.

But how does the compiler understand that? Where are the information about the other arrays stored in memory?

source for screenshots - mycodeschool youtube channel

Enzio
  • 799
  • 13
  • 32
  • Compiler knows about the array dimensions. It doesn't know if they have been decayed to pointers. – Eugene Sh. Jun 05 '17 at 17:17
  • When you write `&C[1]`, for example, the compiler translates that into adding `(address of C) + 16`, if `sizeof(int)` is 4. – Dietrich Epp Jun 05 '17 at 17:20
  • There is only a separate variable at 900 if you have declared a pointer in addition to the array. The array does not need any other information. – stark Jun 05 '17 at 17:21

1 Answers1

0

When you declare a static array tab[x][y] the compiler will use tab[x * y] and calculate the right offset. It doesn't work like double pointers, like char **tab. In that case you have an array of pointers, each one pointing on an array of char.

When you declare a function using a static array, you have to specify the size of the dimensions excepted the last. For example:

Void fn(char t[4][5][6][])

And t[x][y][z][a] will be replaced per t[x × 4 × 5 × 6 + y × 5 × 6 + z × 6 + a]