For example, consider the declarations of a three-dimensional array of size 2 x 3 x 4 given below.
int a[2][3][4];
We can interpret a as an array having three elements each of which is a matrix of size 3 x 4. Thus, contiguous memory is allocated for three matrices a [0], a [1] and a [2].
As we know, array name a is a pointer to its beginning. Actually, it is a pointer to a matrix of size 3 x 4, i. e., (*a) [3] [4]. We can access the ijkth element of this matrix using pointer notation as * (* (* (a+ i) +j )+k) . We can pass this array to a function and access its elements using pointer notation as illustrated in the iarr3d _print function given below.
void iarr3d_print (int (*a) [3] [4], int; m, int n, int p)
{
int i, j, k;
int *pa = **a; /* init int pointer to point to a[0] [0] [0] */
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < p; k++)
printf("%2d ", *pa++); /* access elem & incr ptr */
printf("\n");
}
printf("\n\n");
}
}
Note that the pointer pa is initialized to **a. As a is a pointer to array a [oJ of size 3 x 4, *a is a pointer to vector a [0] [0] having four elements and dereferencing it once more as **a, we get a pointer to element a [0] [0] [0]. Alternatively, we can write this initialization as
int *pa= (int*) a; /* init int pointer to point to a[0] [0] [0] */
Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses