Pointer to Three-Dimensional Arrays in C

If elements of an array are two-dimensional arrays, the array is called a three-dimensional array. Therefore, a three-dimensional array may be considered as an array of matrices. Let Arm be a 3-dimensional array or an array of matrices. The declaration of pointer and its initialization is carried out as given below.

int Arm[2] [3] [4] ;

int (*pArm) [3] [4] = Arm ;

 In the above declaration, pArm is the pointer identifier and Arm is the address of the three-dimensional array int Arm [2] [3] [4]. Therefore, pArm = Arm, because both are pointers to the first element of the array. The elements of the array may be accessed either through index values or through pointers using offset values. Thus, an element of an array with index values i,j, and k may be accessed through the [ ] operator by the following expression:

 Arm[i] [j] [k]

 The elements may also be accessed by the following expressions in pointer notation. In these expressions, the values of offsets i,j, k are same as the index values. The array name Arm is a constant pointer to the array.

 *(*(*(Arm +i) + j) + k)

 The elements may also be accessed by the pointer pArm as declared above.

 *(*(*(pArm +i) + j) + k) Program pointer to a three-dimensional array and its output.

 #include <stdio.h>

int main () {

  int A[2] [3] [4] = {{1,2,3,4 ,10,11,12,13, 20,21,22,23}, 

                      { 30,31,32,33, 40,41,42,43, 50, 51, 52,53 }} ;

  int(*pA) [3] [4] =A ;

  int i, j, k;

  for ( i =0; i<2; i++) { 

    printf("The elements of A[%d] [j] [k] are given below.\n", i);

    for ( j =0; j <3; j++) {

      for ( k =0; k<4; k++)

        printf("%d\t", *(*(*(pA +i) + j) +k));

        printf ("\n");

    } 

  }

  printf ("\n");

   return 0;

}

If elements of an array are two-dimensional arrays, the array is called a three-dimensional array. Therefore, a three-dimensional array may be considered as an array of matrices. Let Arm be a 3-dimensional array or an array of matrices. The declaration of pointer and its initialization is carried out as given below.

1
2
int Arm[2] [3] [4] ;
int (*pArm) [3] [4] = Arm ;

In the above declaration, pArm is the pointer identifier and Arm is the address of the three-dimensional array int Arm [2] [3] [4]. Therefore, pArm = Arm, because both are pointers to the first element of the array. The elements of the array may be accessed either through index values or through pointers using offset values. Thus, an element of an array with index values i,j, and k may be accessed through the [ ] operator by the following expression:

1
Arm[i] [j] [k]

The elements may also be accessed by the following expressions in pointer notation. In these expressions, the values of offsets i,j, k are same as the index values. The array name Arm is a constant pointer to the array.

1
*(*(*(Arm +i) + j) + k)

The elements may also be accessed by the pointer pArm as declared above.

*(*(*(pArm +i) + j) + k) Program pointer to a three-dimensional array and its output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main () {
  int A[2] [3] [4] = {{1,2,3,4 ,10,11,12,13, 20,21,22,23},
                      { 30,31,32,33, 40,41,42,43, 50, 51, 52,53 }} ;
  int(*pA) [3] [4] =A ;
  int i, j, k;
  for ( i =0; i<2; i++) {
    printf("The elements of A[%d] [j] [k] are given below.\n", i);
    for ( j =0; j <3; j++) {
      for ( k =0; k<4; k++)
        printf("%d\t", *(*(*(pA +i) + j) +k));
        printf ("\n");
    }
  }
  printf ("\n");
   return 0;
}

posted on 2022-03-22 13:26  荷树栋  阅读(29)  评论(0编辑  收藏  举报

导航