What will be output if you will execute following code?
union emp{
char *name;
int id;
};
int main(){
static union emp e1={"A"},e2={"B"},e3={"C"};
union emp(*array[])={&e1,&e2,&e3};
union emp(*(*ptr)[3])=&array;
printf("%s ",(*(*ptr+2))->name);
return 0;
return 0;
}
Output: C
Explanation:
In this example:
e1, e2, e3: They are variables of union emp.
array []:It is one dimensional array of size thee and its content are address of union emp.
ptr: It is pointer to array of union.
(*(*ptr+2))->name
=(*(*&array+2))->name //ptr=&array
=(*(array+2))->name //from rule *&p=p
=array[2]->name //from rule *(p+i)=p[i]
=(&e3)->name //array[2]=&e3
=*(&e3).name //from rule ->= (*).
=e3.name //from rule *&p=p
=”C”
No comments:
Post a Comment