Pointers and Multidimensional Arrays in C
Pointers and Multidimensional Arrays in C
Simply, the name of the array too points to the address of the 0th element. So, you can also
use this expression −
int *x = a;
Example
Since the value of the pointer increments by the size of the data type, "x++" moves the
pointer to the next element in the array.
#include <stdio.h>
int main(){
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int *ptr = arr;
while (i < length){
printf("arr[%d]: %d \n", i, *(ptr + i));
i++;
}
return 0;
}
Output
When you run this code, it will produce the following output −
arr[0]: 1
arr[1]: 2
arr[2]: 3
arr[3]: 4
arr[4]: 5
1
int arr[2][2];
Such an array is represented as −
Col0 Col1 Col2
Value 1 2 3 4 5 6 7 8 9 10 11 12
Address 1000 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044
We will assign the address of the first element of the array num to the pointer ptr using the
address of & operator.
int *ptr = &arr[0][0];
Example 1
If the pointer is incremented by 1, it moves to the next address. All the 12 elements in the
"3×4" array can be accessed in a loop as follows −
#include <stdio.h>
int main(){
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
};
// pointer ptr pointing at array num
int *ptr = &arr[0][0];
int i, j, k = 0;
// print the elements of the array num via pointer ptr
for (i = 0; i < 3; i++){
for (j = 0; j < 4; j++){
printf("%d ", *(ptr + k));
k++;
}
2
printf("\n");
}
return 0;
}
Output
When you run this code, it will produce the following output −
1 2 3 4
5 6 7 8
9 10 11 12
In general, the address of any element of the array by with the use the following formula −
add of element at ith row and jth col = baseAddress + [(i * no_of_cols + j) *
sizeof(array_type)]
In our 3×4 array,
add of arr[2][4] = 1000 + (2*4 + 2)*4 = 1044
You can refer to the above figure and it confirms that the address of "arr[3][4]" is 1044.
Example 2
Use the dereference pointer to fetch the value at the address. Let us use this formula to
traverse the array with the help of its pointer −
#include <stdio.h>
int main(){
// 2d array
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// pointer
int *ptr = &arr[0][0];
return 0;
}
Output
When you run this code, it will produce the following output −
1 2 3 4
5 6 7 8
9 10 11 12
3
Pointers and Three-dimensional Arrays
A three-dimensional array is an array of two-dimensional arrays. Such an array is declared
with three subscripts −
int arr [x] [y] [j];
This array can be considered as "x" number of layers of tables, each table having "x" rows
and "y" number of columns.
An example of a 3D array is −
int arr[3][3][3] ={
{ {11, 12, 13}, {14, 15, 16}, {17, 18, 19} },
{ {21, 22, 23}, {24, 25, 26}, {27, 28, 29} },
{ {31, 32, 33}, {34, 35, 36}, {37, 38, 39} },
};
A pointer to the 3D array can be declared as −
int * ptr = &arr[0][0][0];
Knowing that, the name of the array itself is the address of 0th element, we can write the
pointer of a 3D array as −
int * ptr = arr;
Each layer of "x" rows and "y" columns occupies −
x * y * sizeof(data_type)
Number of bytes. Assuming that the memory allocated to the 3D array "arr" as declared
above starts from the address 1000, the second layer (with "i = 1") starts at
1000 + (3 × 3) × 4 = 1036 byte position.
ptr = Base address of 3D array arr
If JMAX is the number of rows and KMAX is the number of columns, then the address of
the element at the 0th row and the 0th column of the 1st slice is −
arr[1][0][0] = ptr + (1 * JMAX * KMAX)
The formula to obtain the value of an element at the jth row and kth column of the ith slice
can be given as −
arr[i][j][k] = *(ptr + (i * JMAX*KMAX) + (j*KMAX + k))
Example: Printing a 3D Array using Pointer Dereferencing
Let us use this formula to print the 3D array with the help of the pointer dereferencing −
#include <stdio.h>
int main(){
int i, j, k;
int arr[3][3][3] = {
{ {11, 12, 13}, {14, 15, 16}, {17, 18, 19} },
{ {21, 22, 23}, {24, 25, 26}, {27, 28, 29} },
{ {31, 32, 33}, {34, 35, 36}, {37, 38, 39} },
};
4
printf("\n");
}
return 0;
}
Output
When you run this code, it will produce the following output −
11 12 13
14 15 16
17 18 19
21 22 23
24 25 26
27 28 29
31 32 33
34 35 36
37 38 39
In general, accessing an array with a pointer is quite similar to accessing an array with
subscript representation. The main difference between the two is that the subscripted
declaration of an array allocates the memory statically, whereas we can use pointers for
dynamic memory allocation.