Pointer to a Double Dimension Array of Integers
In this section, you are going to learn
How to define and use Pointer to Double Dimension Arrays ?
type ( *ptr ) [ row_size ] [ col_size ];
An example is as below
int a[2][4] = { {1, 2, 3, 4}, {10, 20, 30, 40} };
int (*ptr)[2][4];
ptr = &a;
Pointer to an Array when incremented, increments by size of the array to which it is pointing to !
Pointer to an Array when decremented, decrements by size of the array to which it is pointing to !
Let us see how it is done. See below
Step 1 : Define a double dimension array
int a[2][4] = { {1, 2, 3, 4}, {10, 20, 30, 40} };
Step 2 : Define a pointer to an array
int (*ptr)[2][4];
Step 3 : Let pointer to an array, point to double dimension array
ptr = &a;
Step 4 : Check pointer arithmetic
printf("Before Increment : ptr = %lx\n", (unsigned long int) ptr);
ptr++;
printf("After Increment : ptr = %lx\n", (unsigned long int) ptr);
See full program below
#include <stdio.h>
int main(void)
{
int a[2][4] = { {1, 2, 3, 4}, {10, 20, 30, 40} };
int (*ptr)[2][4];
ptr = &a;
printf("Before Increment : ptr = %lx\n", (unsigned long int) ptr);
ptr++;
printf("After Increment : ptr = %lx\n", (unsigned long int) ptr);
return 0;
}
Output is as below
Before Increment : ptr = 7ffcac29b980
After Increment : ptr = 7ffcac29b9a0
Observe that difference is 32 !
Size of two dimensional array is 32 Bytes
Hence incrementing
ptr
will increment by 32 Bytes
When Pointer to an Array is passed as argument, Prototype of function should match the type properly !
Let us see how it is done. See below
Step 1 : Define a double dimension array
int a[2][4] = {
{1, 2, 3, 4},
{10, 20, 30, 40}
};
Step 2 : Define a pointer to an array
int (*ptr)[2][4];
Step 3 : Let pointer to an array, point to double dimension array
ptr = &a;
Step 4 : Pass
ptr
as argument to functionfun
fun(ptr);
Step 5 : Define function
fun
void fun( int (*p)[2][4] )
{
}
Step 6 : Print individual integers inside
fun
void fun( int (*p)[2][4] )
{
//print Individual integers
printf("Print integers using %%d\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", (*p)[i][j]);
}
printf("\n");
}
}
This is easy to understand ! Let us derive the rules
Rule 1 : Base Rule inside
main
ptr = &a
Rule 2 : Base Rule inside
fun
p = ptr
Rule 3 : From Rule 1 and Rule 2
p = &a
Rule 4 : Move & from RHS to LHS. This becomes * on LHS
*p = a;
Rule 5 :
a
is equal to&a[0]
*p = &a[0]
Rule 6 : Move & from RHS to LHS. This becomes * on LHS
**p = a[0]
Rule 7 : * and [ ] can be used interchangeably
(*p)[0] = a[0]
Rule 8 : Extending Rule 7
(*p)[i] = a[i]
Rule 9 :
a[0]
is equal to&a[0][0]
(*p)[0] = &a[0][0]
Rule 10 : Move & from RHS to LHS. This becomes * on LHS
(**p)[0] = a[0][0]
Rule 11 : * and [ ] can be used interchangeably
(*p)[0][0] = a[0][0]
Rule 12 : Extending Rule 11
(*p)[i][j] = a[i][j]
Hence we proved, if a[i][j]
is an integer then (*p)[i][j]
is also an integer
Another way to understand !
int (*p)[2][4]
is the declaration of pointerp
(*p)[ ][ ]
is dereferenced to maximum level. Hence is an integer
Step 7 : Print individual integers inside
fun
void fun( int (*p)[2][4] )
{
//print Individual integers
printf("Print integers using %%d\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", (*p)[i][j]);
}
printf("\n");
}
}
See full program below
#include <stdio.h>
void fun( int (*p)[2][4] )
{
//print Individual integers
printf("Print integers using %%d\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", (*p)[i][j]);
}
printf("\n");
}
}
int main(void)
{
int a[2][4] = {
{1, 2, 3, 4},
{10, 20, 30, 40}
};
int (*ptr)[2][4];
ptr = &a;
fun(ptr);
return 0;
}
Output is as below
Print integers using %d
1 2 3 4
10 20 30 40
When Pointer to an Array is passed as argument by call by reference, Prototype of function should match the type properly !
Let us see how it is done. See below
Step 1 : Define a Double dimension array
int a[2][4] = {
{1, 2, 3, 4},
{10, 20, 30, 40}
};
Step 2 : Define a pointer to an array
int (*ptr)[2][4];
Step 3 : Let pointer to an array, point to double dimension array
ptr = &a;
Step 4 : Pass
ptr
as argument to functionfun
. Call by reference
fun(&ptr);
Step 5 : Define function
fun
and print individual integers
void fun(int (**p)[2][4])
{
// Print individual integers
printf("Print integers using %%d\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", (**p)[i][j]);
}
printf("\n");
}
}
This is easy to understand ! Let us derive the rules
Rule 1 : Base rule in
main
ptr = &a;
Rule 2 : Base rule in
fun
p = &ptr
Rule 2 : Move & from RHS to LHS. This becomes * on LHS
*p = ptr
Rule 3 : Replace
ptr
referring to Rule 1
*p = &a
Rule 4 : Move & from RHS to LHS. This becomes * on LHS
**p = a
Rule 5 :
*
and[ ]
can be used interchangeably
(*p)[0] = a
Rule 6 :
a
is equal to&a[0]
(*p)[0] = &a[0]
Rule 7 : Move & from RHS to LHS. This becomes * on LHS
(**p)[0] = a[0]
Rule 8 : Extending Rule 7
(**p)[i] = a[i]
Rule 9 :
a[0]
is equal to&a[0][0]
(**p)[0] = &a[0][0]
Rule 10 : Move & from RHS to LHS. This becomes * on LHS
(***p)[0] = a[0][0]
Rule 11 : * and [ ] can be used interchangeably
(**p)[0][0] = a[0][0]
Rule 12 : Extending Rule 11
(**p)[i][j] = a[i][j]
Hence we proved if a[i][j]
is a integer then (**p)[i][j]
is also a integer
See full program below
#include <stdio.h>
void fun(int (**p)[2][4])
{
// Print individual integers
printf("Print integers using %%d\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", (**p)[i][j]);
}
printf("\n");
}
}
int main(void)
{
int a[2][4] = {
{1, 2, 3, 4},
{10, 20, 30, 40}
};
int (*ptr)[2][4];
ptr = &a;
fun(&ptr);
return 0;
}
Output is as below
Print integers using %d
1 2 3 4
10 20 30 40
Step 1 : Define a triple dimension array of integers
int a[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{10, 20, 30, 40},
{50, 60, 70, 80},
{90, 100, 110, 120}
},
};
Step 2 : Define a pointer to an array of double dimension
int (*ptr)[3][4];
Step 3 : Let the pointer to point to Double dimension array inside a Double dimension array
ptr = &a[0];
Step 4 : Print the individual integers : Method 1
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("ptr[%d][%d][%d] = %d\n", i, j, k, ptr[i][j][k]);
}
}
}
Step 5 : Print the individual integers : Method 2
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("(*ptr)[%d][%d] = %d\n", j, k, (*ptr)[j][k]);
}
}
ptr++;
}
This is easy to understand ! Let us derive the rules
Rule 1 : Base rule
ptr = &a[0];
Rule 2 : Move & from RHS to LHS. This becomes * on LHS
*ptr = a[0]
Rule 3 :
a[0]
is equal to&a[0][0]
*ptr = &a[0][0]
Rule 4 : Move & from RHS to LHS. This becomes * on LHS
**ptr = a[0][0]
Rule 5 :
*
and[ ]
can be used interchangeably
ptr[0][0] = a[0][0]
Rule 6 : Extending Rule 3
ptr[i][j] = a[i][j]
Rule 7 :
a[0][0]
is equal to&a[0][0][0]
ptr[0][0] = &a[0][0][0]
Rule 8 : Move & from RHS to LHS. This becomes * on LHS
*ptr[0][0] = a[0][0][0]
Rule 9 :
*
and[ ]
can be used interchangeably
ptr[0][0][0] = a[0][0][0]
Rule 10 : Extending Rule 9
ptr[i][j][k] = a[i][j][k]
Hence we proved, if a[i][j][k]
is an integer then p[i][j][k]
is also also an integer
See full program below
#include <stdio.h>
int main(void)
{
int a[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{10, 20, 30, 40},
{50, 60, 70, 80},
{90, 100, 110, 120}
},
};
int (*ptr)[3][4];
ptr = &a[0];
//Method 1 : print Integers
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("ptr[%d][%d][%d] = %d\n", i, j, k, ptr[i][j][k]);
}
}
}
printf("\n");
//Method 2 : print Integers
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("(*ptr)[%d][%d] = %d\n", j, k, (*ptr)[j][k]);
}
}
ptr++;
}
return 0;
}
Output is as below
ptr[0][0][0] = 1
ptr[0][0][1] = 2
ptr[0][0][2] = 3
ptr[0][0][3] = 4
ptr[0][1][0] = 5
ptr[0][1][1] = 6
ptr[0][1][2] = 7
ptr[0][1][3] = 8
ptr[0][2][0] = 9
ptr[0][2][1] = 10
ptr[0][2][2] = 11
ptr[0][2][3] = 12
ptr[1][0][0] = 10
ptr[1][0][1] = 20
ptr[1][0][2] = 30
ptr[1][0][3] = 40
ptr[1][1][0] = 50
ptr[1][1][1] = 60
ptr[1][1][2] = 70
ptr[1][1][3] = 80
ptr[1][2][0] = 90
ptr[1][2][1] = 100
ptr[1][2][2] = 110
ptr[1][2][3] = 120
(*ptr)[0][0] = 1
(*ptr)[0][1] = 2
(*ptr)[0][2] = 3
(*ptr)[0][3] = 4
(*ptr)[1][0] = 5
(*ptr)[1][1] = 6
(*ptr)[1][2] = 7
(*ptr)[1][3] = 8
(*ptr)[2][0] = 9
(*ptr)[2][1] = 10
(*ptr)[2][2] = 11
(*ptr)[2][3] = 12
(*ptr)[0][0] = 10
(*ptr)[0][1] = 20
(*ptr)[0][2] = 30
(*ptr)[0][3] = 40
(*ptr)[1][0] = 50
(*ptr)[1][1] = 60
(*ptr)[1][2] = 70
(*ptr)[1][3] = 80
(*ptr)[2][0] = 90
(*ptr)[2][1] = 100
(*ptr)[2][2] = 110
(*ptr)[2][3] = 120
Current Module
Previous Module
Next Module
Other Modules