clanguage07@gmail.com

clanguage07@gmail.com

Sunday, September 8, 2013

Pointer to array of structure in c programming


Pointer to array of structure: A pointer to an array which contents are pointer to structure is know pointer to array of structure.

What will be output if you will execute following code?


#include<stdio.h>

struct emp{
char *name;
int id;
};

int main(){

static struct emp e1={"A",1},e2={"B",2},e3={"C",3};
struct emp(*array[])={&e1,&e2,&e3};
struct emp(*(*ptr)[3])=&array;

printf("%s %d",(**(*ptr+1)).name,(*(*ptr+1))->id);


return 0;
}

Output: B 2
Explanation:

(**(*ptr+1)).name
=(**(*&array+1)).name //ptr=&array
=(**(array+1)).name //from rule *&p =p
=(*array[1]).name //from rule *(p+i)=p[i]
=(*&e2).name //array[1]=&e2
=e2.name=”B” //from rule *&p =p
(*(*ptr+1))->id
=(**(*ptr+1)).id //from rule -> = (*).
=e2.id=2

No comments:

Post a Comment

GET MORE INFORMATION

http://ads.qadservice.com/t?id=c2168e05-8974-4816-872a-91936ff7379d&size=1024x768&drct=true