Pointer to array of union in c programming | CTechnotips


Pointer to array of union:  A pointer to an array which contents is pointer to union is known as pointer to array of union.


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

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”

0 comments:

Post a Comment