Pointer to array of union in c programming

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”



Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
C tutorial
Post a Comment