Pointer to array of array in c


Examples of pointer to array of array in c:


What will be output if you will execute following code?


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

static float farray[][3]={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f};
float (*array[3])[3]={&farray[0],&farray[1],&farray[2]};
float (*(*ptr)[])[3]=&array;

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


return 0;
}

Output: 5.000000
Explanation:

In this example:

farray [][3]: It is two dimension array and its content are float constants. 

array [3]:It is one dimension array and its content are address of such one dimension array which content are float constant.

ptr: It is pointer to one dimension array which content are address of such one dimension array which content are float constant. 

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

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

It is 1*(3) +2=5th element of farray starting from zero which is 5.0f

No comments:

Post a Comment