Govt Exams
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.
A structure allows grouping of different data types. Padding may be added by the compiler for alignment.
Structures must be initialized at declaration time using curly braces. Assignment of initializer list is not valid after declaration.
The correct syntax for typedef struct is: typedef struct { members } StructName;
The arrow operator (->) is used to access structure members through a pointer. The dot operator is used with direct variables.
The size of a union equals the size of its largest member. Here, int is 4 bytes (largest), so union size is 4 bytes.
Union members share the same memory location, and the union size equals the size of its largest member.
struct Student { int roll; char name[50]; };
Correct allocation requires pointer declaration, proper cast, and multiplying n with sizeof(struct Student).
Pass by value creates a complete copy of the structure on the function's stack frame.