Programming in C: Unit IV: Structures and Union

Arrays of Union Variables

with Example C Program

Like structures we can also have an array of union variables. However, because of the problem of new data overwriting existing data in the other fields, the program may not display the accurate results.

ARRAYS OF UNION VARIABLES

Like structures we can also have an array of union variables. However, because of the problem of new data overwriting existing data in the other fields, the program may not display the accurate results.

#include <stdio.h>

union POINT

{

int x, y;

};

int main()

{

int i;

union POINT points [3];

}

points [0].x = 2;

points [0].y = 3;

points [1] .x = 4;

points [1].y = 5;

points [2].x = 6;

points [2].y = 7;

for (i=0;i<3;i++)

printf("\n Co-ordinates of Point [%d] are %d and %d", i, points [i].x, points [i].y);

return 0;

}

Output

Co-ordinates of Point [0] are 3 and 3

Co-ordinates of Point [1] are 5 and 5

Co-ordinates of Point [2] are 7 and 7

Programming in C: Unit IV: Structures and Union : Tag: : with Example C Program - Arrays of Union Variables