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?
here i am thinking of 2D array b as array of 1D arrays
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.
- How is the expression b[i] evaluated in case when b is 2D array?
...
- What does b evaluate to if b is a 2D array?
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 type int(*)[3]
.
- How is the expression b[i][j] evaluated in case when b is 2D array?
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 number i
.
- At this position we have a 1D array of type
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 expression tmp[j]
, which as always decays into *(tmp+j)
, resulting in an int
.
Essentially the whole expression can be treated as *(*(b+i) + j)
.
- 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 ?
As explained above, it treats it as an array of arrays. For example, you can iterate over a 2D array by using array pointers:
#include <stdio.h>
void print_array (int array[3])
{
printf("%d %d %d\n", array[0], array[1], array[2]);
}
int main (void)
{
int b[2][3]={ {1,2,3},{4,5,6} };
const size_t b_size = sizeof b / sizeof *b;
for(int(*ptr)[3] = b; ptr < b+b_size; ptr++)
{
print_array(*ptr);
}
}
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,
int x[10];
is an array containing 10 int
s. Similarly,
int x[10][10];
can be considered as an array which contains 10 arrays, each of which contains 10 int
s.
How is the expression b[i]
evaluated in case when b is 2D array?
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 the i
th element (in other words, array) of b
, which is the same with the address of the first element of the i
th array.
How is the expression b[i][j]
evaluated in case when b is 2D array?
b[i][j]
is evaluated as *(*(b+i) + j)
. This indicates choosing the i
th element of b
(in other words, the i
th row), and from this, choosing the j
th element (in other words, the j
th column).
How to access elements of a Multi-dimensional Array in C using pointers.
Consider having a pointer int **x
, which has been dynamically allocated and shows at some memory part, or simply a 2D array int[ROWS][COLS]
. Then you can access any element by using *(*(x+i) + j)
or x[i][j]
, where i
are the rows and j
are the columns.
However, note that a double pointer is not the same as a 2D array.
What does b
evaluates to if b
is 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 as b[0]
, as well as b[0][0]
.