Govt Exams
If B is embedded in A: a_var.b_var.member. If B is a pointer in A: a_var->b_var->member or a_var.b_ptr->member.
Structures can be passed by value (copying the entire structure) or by pointer (passing address) for efficiency.
C does not support direct comparison of structures using ==. You must compare members individually or use memcmp().
struct Flags {
unsigned int flag1 : 1;
unsigned int flag2 : 3;
unsigned int flag3 : 4;
};
What is the minimum size of this structure?
Bit fields must fit within an underlying integer type. Total bits = 8, which requires at least 4 bytes (one int on most systems).
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.
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.
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.
Use dot operator (.) for direct structure variables and arrow operator (->) for pointers, or combinations for nested access.
Reading a union member that wasn't last written gives unpredictable results (garbage value or leftover bits from previous member).
typedef struct creates a type alias, allowing you to declare variables without using the 'struct' keyword each time.