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>
#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;
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