Accessing multi-dimensional arrays in C using pointer notation
arrays, c, multidimensional-array, pointers
Solution
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);
}
}
Problem
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?