Entrance Exams
Govt. Exams
union Test {
int a[5];
char b[10];
double c;
}
Union size equals the largest member. int[5] = 20 bytes, char[10] = 10 bytes, double = 8 bytes. Maximum is 20 bytes.
Both (*ptr).member and ptr->member are valid. Option B uses explicit dereferencing, while the arrow operator is syntactic sugar for this.
union Data {
int a;
char b;
};
union Data d;
d.a = 257;
printf("%d %c", d.a, d.b);
Union shares memory. 257 in int format overwrites the char. On a little-endian system, only the lower byte (1) is stored in the char portion.
In a union, all members occupy the same memory space, while struct members have individual memory locations.
struct Data {
int a;
char b;
};
struct Data d = {10, 'A'};
Which method of initialization is used here?
Values are assigned in the order they appear in the structure definition, which is positional initialization.
struct Point {
char c;
int x;
double y;
}
char(1) + padding(3) + int(4) + double(8) = 16 bytes minimum, but alignment to double boundary makes it 24 bytes.
A structure allows grouping of different data types. Padding may be added by the compiler for alignment.
Use dot operator (.) for direct structure variables and arrow operator (->) for pointers, or combinations for nested access.
Bit fields save memory by storing multiple values in single bytes, but behavior varies by compiler/platform, and you cannot take addresses of bit field members.
Structures must be initialized at declaration time using curly braces. Assignment of initializer list is not valid after declaration.