Demo: A Tag Tells You What Is Stored
Keep track of which member is stored
A union doesn't record which member you wrote last. A separate kind value can do that job. Here a struct holds both the tag and the union, so the program checks whether the ticket carries a tool count or a grade before printing.
c
#include <stdio.h>
enum Kind { TOOL_COUNT, QUALITY_GRADE };
struct Ticket {
enum Kind kind;
union {
int count;
char grade;
} value;
};
int main(void) {
struct Ticket ticket = {.kind = TOOL_COUNT, .value.count = 4};
if (ticket.kind == TOOL_COUNT) {
printf("Tools: %d\n", ticket.value.count);
} else {
printf("Grade: %c\n", ticket.value.grade);
}
return 0;
}
Try the grade case by changing kind to QUALITY_GRADE and the stored member to grade = 'A'. Change both together. A grade tag paired with a count would send the program to the wrong member.