Complete the code to declare a structure named Person.
struct Person {
char name[50];
int age;
float height;
};
struct [1] person1;The keyword Person is the name of the structure type declared earlier. We use it to declare a variable of that type.
Complete the code to declare a union named Data.
union [1] { int i; float f; char str[20]; }; union Data data1;
The union is named Data. We use this name to declare variables of this union type.
Fix the error in accessing the union member to assign a value.
union Data data1; data1.[1] = 10;
The union Data has a member named i for integer values. We assign 10 to data1.i.
Fill both blanks to create a structure and a union variable.
struct [1] person2; union [2] data2;
The structure is named Person and the union is named Data. We use these names to declare variables.
Fill all three blanks to create a dictionary comprehension-like code in C style (simulate with comments) comparing sizes of structure and union.
/* Size of struct Person: sizeof(struct [1]) */ /* Size of union Data: sizeof(union [2]) */ /* The size difference is because [3] */
The size of a structure is the sum of all members, so we use Person. The union size is the size of its largest member, so Data. The union shares memory among members, which is why its size is smaller.