Saturday, 25 July 2015

Pointer to array of pointer to string in c programming

Pointer to array of pointer to string in c programming



Pointer to array of pointer to string: A pointer to an array which contents are pointer to string.


Example of Pointer to array of pointer to string:


What will be output if you will execute following code?


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

static char *s[3]={"math","phy","che"};
typedef char *( *ppp)[3];
static ppp p1=&s,p2=&s,p3=&s;
char * (*(*array[3]))[3]={&p1,&p2,&p3};
char * (*(*(*ptr)[3]))[3]=&array;

p2+=1;
p3+=2;

printf("%s",(***ptr[0])[2]);


return 0;
}

Output: che
Explanation: 

Here
ptr: is pointer to array of pointer to string.

P1, p2, p3: are pointers to array of string.

array[3]: is array which contain pointer to array of string.

Pictorial representation:

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

As we know p[i]=*(p+i)

(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]
=(***(&array))[2] //ptr=&array
=(**array)[2] //From rule *&p=p
=(**(&p1))[2] //array=&p1
=(*p1)[2]
=(*&s)[2] //p1=&s
=s[2]=”che”

No comments:

Post a Comment