Pointer to three dimensional array in c programming


Examples of pointers to 3 dimensional array:


#include<stdio.h>
int main(){

const array[2][3][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int const (*ptr)[2][3][3]=&array;

printf("%d ",*(*(*ptr)[1]+2));


return 0;
}

Output: 11
Explanation:

In this example:
array [2][3][3]:It is three dimensional array and its content are constant integers.
ptr: It is pointer to such three dimensional array whose content are constant integer.

Pictorial representation:

Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

*(*(*ptr) [1] +2)
=*(*(*&array) [1] +2)
=*(*array [1] +2)
=*(array [1] [0] +2)
=array [1] [0] [2] 

I.e. array element at the 1*(3*3) +0(3) + 2=11th position starting from zero which is 11.

No comments:

Post a Comment