Accessing multidimensional arrays in C using a pointer
Let a be a one-dimensional array of 10 integers as shown below
int a[10];
and for the above array the expression
a[i]
evaluates to * (a + i) and gives the value stored at index i , for example.
int a[5]={1,2,3,4,5};
here a [3] is evaluated as * (a + 3) , which is 4 , ie the value of the element stored at the third index of the array a . But in case of 2D array like this,
int b[2][3]={ {1,2,3},{4,5,6} };
(here I am considering 2D array b as an array of 1D arrays, i.e. b is a 1D array containing 2 elements, each of which is a 1D array of 3 int) if we use a single index operator with an array name as
b[i]
the above expression for i = 1 gives the address where item 4 is stored, so
- How is the expression b [i] evaluated when b is a two-dimensional array?
- How is b [i] [j] expressed when b is a two-dimensional array?
- How to access the elements of a multidimensional array in C using pointers. Here I would like to know how does the compiler handle a multidimensional array internally?
- What does b do if b is a two-dimensional array?
source to share
here i am thinking of 2D array b as array of 1D arrays
This is the correct way to approach it, because this is truly what it is. The C standard doesn't really define multidimensional arrays as some special cases, but they are rather possible because an array is a type like any other. This way we can have an array of arrays.
- How does the expression b [i] work when b is a two-dimensional matrix?
...
- What does b evaluate if b is a two-dimensional array?
As with any array, b
when used in an expression, decays to a pointer to the first element. b[i]
therefore it is equivalent *(b+i)
as for any other array expression.
In this case, b
decays to an array pointer of type int(*)[3]
.
- How is the expression b [i] [j] executed when b is a two-dimensional array?
-
b
is used in an expression, so in that expression it decays to a pointer to the first element, which is an array pointer to the first array. -
b[i]
causes the pointer arithmetic to be applied to the array pointer, which is equivalent*(b+i)
. This gives the array numberi
. - At this position, we have a 1D array of type
int[3]
. Since this array is part of another array, it itself has no identifier. But, for the sake of illustration, let's pretend it gets a temporary name "tmp
". Then we would have an expressiontmp[j]
that, as always, splits into*(tmp+j)
, as a result we getint
.
Essentially, the whole expression can be viewed as *(*(b+i) + j)
.
- How to access the elements of a multidimensional array in C using pointers. Here I would like to know how does the compiler handle a multidimensional array internally?
As explained above, it treats it as an array of arrays. For example, you can iterate over a 2D array 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);
}
}
source to share
A 2D array can be thought of as an array of arrays, or in other words, an array that has arrays as elements. For example,
int x[10];
- an array containing 10 int
s. Similarly,
int x[10][10];
can be thought of as an array containing 10 arrays, each containing 10 int
s.
How is the expression evaluated
b[i]
when b is a two-dimensional array?
When b
is a two-dimensional array, b
also a pointer to the first element of the array. Therefore, it b[i]
evaluates as *(b+i)
and gives the address of the i
th element (in other words, an array) b
, which is the same as the address of the first element of the i
th array.
How is the expression evaluated
b[i][j]
when b is a two-dimensional array?
b[i][j]
evaluated as *(*(b+i) + j)
. This indicates the selection of the i
th item b
(in other words, the i
th row), and from that, selecting the j
th item (in other words, the j
th column).
How to access the elements of a multidimensional array in C using pointers.
Consider having a pointer int **x
that has been dynamically allocated and shown in some part of memory, or just a 2D array int[ROWS][COLS]
. Then you can access any element with *(*(x+i) + j)
or x[i][j]
, where i
are rows and j
are columns.
Note, however, that a double pointer is not the same as a 2D array .
What evaluates
b
if itb
is a two-dimensional array?
When b
is a two-dimensional array, b
also a pointer to the first element of the array. So it is the same as b[0]
and also b[0][0]
.
source to share