Home > Enterprise >  Multi-dimensional array as a pointer - where are addresses of subarrays stored?
Multi-dimensional array as a pointer - where are addresses of subarrays stored?

Time:12-27

In the tutorial video about pointers (enter image description here

The following facts about that code were given:

  • Printing B (or &B[0]) would return 400 (the address of the first sub-array)
  • Printing *B (or B[0] or &B[0][0]) would return 400 (the address of the first element in the first sub-array)

I struggle a bit with an understanding of that. How can B and *B have the same value? We can clearly see in the illustration that under address 400 there is a value 2. If B = 400, then *B = 2!

My understanding is that the value of B would be the address of the first element of the outer array in the memory. Then, the *B would return the address of the first sub-array in the memory.

According to the illustration, B (not shown) has a value 400. Then, under 400, there is the first value of the first sub-array. Where are the addresses of the sub-arrays stored?

CodePudding user response:

B and *B do not have the same value, they have different types, namely int[2][3] and int[3], but they have the same address in memory, ie: 400 as shown.

Think of B as a building and B[0] as the ground floor. They do not have the same value (in $) but they have the same address (and zip code).

This comparison works for passing B or B[0] to a function (delivery company): you are not passing the actual building, but merely its street address and the type name.

  • Related