Let a be an one dimensional array of 10 integers as shown below
int a[10];
and for above declared array the expression
a[i]
is evaluated as *(a+i) and gives the value stored at the ith index,e.g.
int a[5]={1,2,3,4,5};
here a[3] is evaluated as *(a+3) which is equal to 4 ,i.e. the value of element stored at 3rd index of array a. But in case of a 2D array like this one
int b[2][3]={ {1,2,3},{4,5,6} };
(here I am thinking of 2D array b as array of 1D arrays,i.e. b is a 1D array having 2 elements,each element itself being an 1D array of 3 int) if we use single subscript operator with array name as
b[i]
the above expression for i=1 gives address where the element 4 is stored,so
- How is the expression b[i] evaluated in case when b is 2D array?
- How is the expression b[i][j] evaluated in case when b is 2D array?
- How to access elements of a Multi-dimensional Array in C using pointers. Here I would like to know about how compiler treats a Multi-dimensional Array internally ?
- What does b evaluate to if b is a 2D array?
That's the correct way to approach this, because that's indeed what it is. The C standard does not actually specify multi-dimensional arrays as some special case, but they are rather possible because an array is a type like any other. So we can have an array of arrays.
As for any array,
b
when used in an expression, decays into a pointer to the first element.b[i]
is therefore equivalent to*(b+i)
, as for any other array expression.In this case
b
decays into an array pointer of typeint(*)[3]
.b
is used in an expression, so in this expression it decays into a pointer to the first element, which is an array pointer to the first array.b[i]
causes pointer arithmetic to get applied to the array pointer, equivalent to*(b+i)
. This gives array numberi
.int[3]
. Since this array is part of another array, it has no identifier by itself. But for the sake of illustration, lets pretend it gets a temporary name "tmp
". We would then have the expressiontmp[j]
, which as always decays into*(tmp+j)
, resulting in anint
.Essentially the whole expression can be treated as
*(*(b+i) + j)
.As explained above, it treats it as an array of arrays. For example, you can iterate over a 2D array by using array pointers:
A 2D array can be thought of as an array of arrays, or in other words, an array which has arrays as elements. For example,
is an array containing 10
int
s. Similarly,can be considered as an array which contains 10 arrays, each of which contains 10
int
s.When
b
is a 2D array,b
is also a pointer to the first element of the array. Therefore,b[i]
is evaluated as*(b+i)
and gives the address of thei
th element (in other words, array) ofb
, which is the same with the address of the first element of thei
th array.b[i][j]
is evaluated as*(*(b+i) + j)
. This indicates choosing thei
th element ofb
(in other words, thei
th row), and from this, choosing thej
th element (in other words, thej
th column).Consider having a pointer
int **x
, which has been dynamically allocated and shows at some memory part, or simply a 2D arrayint[ROWS][COLS]
. Then you can access any element by using*(*(x+i) + j)
orx[i][j]
, wherei
are the rows andj
are the columns.However, note that a double pointer is not the same as a 2D array.
When
b
is a 2D array,b
is also a pointer to the first element of the array. So it is the same asb[0]
, as well asb[0][0]
.