ACompressed recap
🧾
A struct bundles members of different types; reach them with . or, through a pointer, ->. Structs are copied on assignment and when passed; pass a pointer to modify. typedef renames types. A self-referential struct holds a pointer to its own type. Padding aligns each member to a multiple of its size and rounds the total to the largest member; reordering members can shrink a struct. A union overlays its members: size = largest member, rounded for alignment.
BMerged rule card
1.6 Structures, unions, typedef; self-referential structures; structure size and padding
- struct T { members }; access s.m or p->m ≡ (*p).m.
- Struct assignment and argument passing copy all members.
- typedef gives a type a new name.
- Self-referential: struct T { …; struct T *next; } (pointer only).
- Padding: member at multiple of its size; total multiple of largest member.
- union: members overlap; size = largest member rounded for alignment.
CInterleaved GATE-style questions
Q1NAT1 markPadding
Size of struct { char a; char b; int c; } (4-byte int, natural alignment)?
Numerical answer: ________
Q2NAT2 marksPadding
Size of struct { char a; int c; char b; } under the same rules?
Numerical answer: ________
Q3MCQ2 marksOutput
What does this print?
typedef struct {
char name[8];
int age;
} Person;
int main(void)
{
Person team[2] = {{"Asha", 21}, {"Ravi", 23}};
Person *p = team;
p++;
printf("%s %d %u\n", p->name, (p - 1)->age, sizeof(team));
return 0;
}
- (A)
Asha 23 24 - (B)
Ravi 21 24 - (C)
Ravi 23 24 - (D)
Ravi 21 16
Q4MCQ1 markCopying
A function void f(struct point p) sets p.x = 0. After f(a), a.x is:
- (A) undefined
- (B) unchanged
- (C) a compile error
- (D) 0
Q5MCQ1 markUnion
A union with members double d; int i; char c; (8-byte double) has size:
- (A) 13
- (B) 16
- (C) 8
- (D) 4
Q6NAT2 marksPointer step
For struct item { char code; double price; short qty; } (natural alignment), how many bytes does p + 1 move a struct item *p?
Numerical answer: ________
Q7MSQ2 marksSyntax
With struct s { int v; struct s *n; } x, *p = &x;, which are valid expressions?
- (A)
(*p).v - (B)
p->v - (C)
p->n->v(if p->n points to a struct s) - (D)
x->v
Q8MCQ2 marksOutput
What does this print?
struct link {
int value;
struct link *next;
};
int main(void)
{
struct link c = {30, NULL};
struct link b = {20, &c};
struct link a = {10, &b};
printf("%d %d %d\n", a.value, a.next->value, a.next->next->value);
printf("%u\n", sizeof(struct link));
return 0;
}
- (A)
10 10 10then8 - (B)
10 20 30then12 - (C)
30 20 10then8 - (D)
10 20 30then8
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: 81 mark
a 0, b 1, c 4–7: 8.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q2 Answer: 122 marks
a 0, c 4–7, b 8, round to 12.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q3 Answer: (B)2 marks
p++ → team[1] (Ravi); (p − 1)->age = 21; sizeof(team) = 2 × 12.
Output
Ravi 21 24
- (A) is wrong: p was moved to team[1].
- (C) is wrong: p − 1 is team[0].
- (D) is wrong: Each Person is 12 bytes.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q4 Answer: (B)1 mark
The struct is copied.
- (A) is wrong: Well defined.
- (C) is wrong: Valid code.
- (D) is wrong: Only the copy changes.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q5 Answer: (C)1 mark
Largest member: 8.
- (A) is wrong: Members overlap.
- (B) is wrong: No extra padding needed.
- (D) is wrong: The double needs 8.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q6 Answer: 242 marks
Size 24 (Lesson 1.6 GATE example).
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q7 Answer: (A) (B) (C)2 marks
x is a struct, not a pointer: use x.v.
- (D) is wrong: -> needs a pointer on its left.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding
Q8 Answer: (D)2 marks
Each next leads one record further; int + pointer = 8 bytes.
Output
10 20 30
8
- (A) is wrong: next moves to b and then c.
- (B) is wrong: int 4 + pointer 4 = 8.
- (C) is wrong: a is printed first.
Re-read: Lesson 1.6 — Structures, unions, typedef; self-referential structures; structure size and padding