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

Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
C tutorial

1 comment:

sk yakub said...

Can you point the difference between 'char** ptr' and 'char **ptr'?