Kit Library / Computer Science / C Programming / Memory Management

⚡ Topic Learning Kit

Structures, Unions & Pointers in C

English 17 leveled MCQs 18 flashcards 9 games Free

Shared by a Veda teacher · Generated with Veda AI

Veda Bites

The whole idea, one bite at a time

Veda Bites are swipeable micro-lessons — each one teaches exactly one idea. The bites below come from this kit — swipe through them in the app.

💡 Key Idea

Struct vs Union: One Memory Rule Changes Everything

Same syntax, totally different memory.

A struct gives every member its own memory; a union makes all members share one block sized to the largest member.

↳ Struct = separate boxes; union = one box shared by many labels.

📖 Definition

Structure: A Named Bundle of Mixed Types

One name, many data types.

↳ A struct is a blueprint; each variable of that type gets its own copy of all members.

📖 Definition

Union: One Memory Block, Many Faces

All members, one address.

↳ A union saves memory but only one member holds a valid value at any moment.

📖 Definition

Pointer: A Variable That Holds an Address

Pointers don't store data — they store where.

↳ & gives an address; * follows it to the value.

💻 Code Example

Accessing Struct Members: Dot vs Arrow

Dot for values, arrow for pointers.

↳ Arrow operator = dereference + member access in one step.

💻 Code Example

Union in Action: Only One Value Survives

Write one member, lose the others.

Writing `d.f` overwrote the bytes that `d.i` was using — so reading `d.i` gives meaningless data.

↳ Never read a union member other than the one last written.

⚠️ Common Mistake

Reading the Wrong Union Member

This bug is silent and deadly.

↳ Unions demand discipline: know which member is live.

⚠️ Common Mistake

Forgetting to Initialize a Pointer

Wild pointers crash programs.

↳ An uninitialized pointer points to nowhere safe.

⚖️ Comparison

When to Choose Struct vs Union

Pick by memory, not by habit.

↳ Struct for records, union for memory-efficient variants.

⭐ Important Fact

sizeof Tells the Truth About Memory

One operator reveals the layout.

↳ Use `sizeof` to verify memory usage — never assume.

⚙️ Process

Passing a Struct to a Function via Pointer

Pointers avoid copying big structs.

↳ Passing by pointer is faster and lets the function modify the original.

🌍 Real World

Where Unions Shine: Embedded Systems

Every byte counts in firmware.

↳ Unions are a memory-saving tool, not a general-purpose container.

🛠️ Application

Pointers + Structs = Linked Lists

Self-referencing structs build data structures.

A struct containing a pointer to its own type is the foundation of linked lists, trees, and graphs.

↳ Self-referential structs are the building blocks of dynamic data structures.

🎯 Exam Tip

Exam Favourite: Size of a Union

This question appears every year.

↳ Union size = largest member; struct size = sum + padding.

🧠 Memory Trick

Remember: Struct = Stack, Union = Share

Two S-words, two rules.

Struct = Stack of separate boxes. Union = United (shared) memory. The first letter tells you the memory rule.

↳ Struct stacks members; union unites them in one block.

Smart notes

What you'll study, topic by topic

1

Structures, Unions & Pointers in C

20 min

Structures group related variables of different types under one name, unions share one memory block among members, and pointers store addresses to enable direct memory access. Together they form the backbone of dynamic d…

  • A structure groups variables of different types under one name.
  • Structure members are accessed with the dot operator (`.`).
  • A pointer to a structure uses the arrow operator (`->`) to access members.

Full explanation, worked examples & memory tricks in the app

Leveled MCQ practice

Try the smart MCQs from this kit

All 17 questions, laddered from warm-up to topper-level, each with its answer and the reasoning behind it.

Warm-up · 6 questions

Q1. Which of the following best describes a structure in C?

Beginner
A A collection of variables of different types under one name
B A memory area shared by all members
C A collection of variables of the same type
D A pointer to another structure
Show the answer

A · A collection of variables of different types under one name

A structure groups variables of different types under a single name, unlike an array which holds same-type elements.

Q2. Given `struct Student s;`, how do you access the `roll` member?

Beginner
A `s->roll`
B `s.roll`
C `s[roll]`
D `*s.roll`
Show the answer

B · `s.roll`

For a structure variable, use the dot operator: `s.roll`.

Q3. What is the size of a union?

Beginner
A Size of the largest member
B Size of the smallest member
C Sum of all member sizes
D Always 4 bytes
Show the answer

A · Size of the largest member

All union members share the same memory, so the union's size is that of its largest member.

Q4. Which operator is used to access a structure member through a pointer?

Beginner
A Ampersand (`&`)
B Dot (`.`)
C Asterisk (`*`)
D Arrow (`->`)
Show the answer

D · Arrow (`->`)

The arrow operator `->` accesses members via a pointer, e.g., `p->member`.

Q5. What does `p->member` mean?

Beginner
A `p.member`
B `*(p.member)`
C `&p.member`
D `(*p).member`
Show the answer

D · `(*p).member`

`p->member` is shorthand for `(*p).member`; the parentheses are required because `.` binds tighter than `*`.

Q6. The size of a union equals the size of its largest member, because all members share the same memory location.

Beginner
A True
B False
Show the answer

A · True

The size of a union equals the size of its largest member, because all members share the same memory location.

Building up · 6 questions

Q7. Why might `sizeof(struct T)` be larger than the sum of its members?

Intermediate
A Because the compiler stores member names
B Because of padding for alignment
C Because structures always add a header
D Because pointers are stored for each member
Show the answer

B · Because of padding for alignment

The compiler inserts padding bytes to align members to natural boundaries, increasing the total size.

Q8. In a union, which member is valid after writing to one member?

Intermediate
A Only the first member
B None of the members
C All members remain valid
D Only the last written member
Show the answer

D · Only the last written member

Union members overlap; writing one overwrites the others. Only the most recently written member is meaningful.

Q9. What is the primary reason to pass a large structure to a function by pointer?

Intermediate
A To avoid copying the entire structure
B To make the code shorter
C To allow the function to modify the original
D To enable recursion
Show the answer

A · To avoid copying the entire structure

Passing by pointer passes only an address, avoiding the cost of copying all bytes. Modification is a separate benefit.

Q10. Which of the following is a self-referential structure?

Intermediate
A `struct Node { int data; };`
B `struct Node { int data; union { int x; }; };`
C `struct Node { int data; struct Node next; };`
D `struct Node { int data; struct Node *next; };`
Show the answer

D · `struct Node { int data; struct Node *next; };`

A self-referential structure contains a pointer to its own type, enabling linked data structures.

Q11. What is the effect of `const struct T *p` in a function parameter?

Intermediate
A The structure is copied to a new location
B The pointer cannot be reassigned, but data can be modified
C The structure is passed by value
D The data pointed to cannot be modified through `p`
Show the answer

D · The data pointed to cannot be modified through `p`

`const struct T *p` means the pointed-to data is read-only through `p`; the pointer itself can change.

Q12. Which of the following is a common use of a union?

Intermediate
A Building a linked list
B Storing a student record with name, roll, and marks
C Passing large structures to functions
D Representing a value that can be an int, float, or string at different times
Show the answer

D · Representing a value that can be an int, float, or string at different times

Unions are ideal for variant data where only one type is active at a time.

Topper level · 5 questions

Q13. What is wrong with `*p.member` when `p` is a pointer to a structure?

Advanced
A It parses as `*(p.member)`, which is incorrect
B It is equivalent to `p->member`
C It dereferences the member value
D It is a syntax error because `*` cannot precede a pointer
Show the answer

A · It parses as `*(p.member)`, which is incorrect

The dot operator has higher precedence than `*`, so `*p.member` means `*(p.member)`, not `(*p).member`.

Q14. How can you reduce padding in a structure?

Advanced
A Declare all members as `char`
B Use a union instead
C Use `#pragma pack(1)` always
D Reorder members from largest to smallest
Show the answer

D · Reorder members from largest to smallest

Placing larger members first often minimizes padding because alignment gaps are reduced.

Q15. What is a tagged union?

Advanced
A A union with a pointer to itself
B A structure containing only a union
C A union that stores all members simultaneously
D A union with a tag field indicating which member is active
Show the answer

D · A union with a tag field indicating which member is active

A tagged union combines a tag (often an enum) with a union to safely track the active member.

Q16. Reading a union member other than the last written one is:

Advanced
A Always safe and portable
B Guaranteed to return zero
C Not portable and usually not what you want
D A compile-time error
Show the answer

C · Not portable and usually not what you want

The bytes are reinterpreted according to the new type; the result depends on representation and is not portable.

Q17. Given the following code snippet, what will be the output when `show(&student1);` is called, assuming `student1` has been initialized with `roll = 10`, `name = 'John'`, and `marks = 85.5`? ```c struct Student { char name[30]; int roll; float marks; }; void show(struct Student *p) { printf("%d", p->roll); } ```

Advanced
A John
B 10
C Error: Cannot access roll
D 85.5
Show the answer

B · 10

The correct option is '10' because the `show` function accesses the `roll` member of the `Student` structure using the pointer `p` and the arrow operator `->`, which correctly dereferences to the `roll` member.

Flashcards

Tap a card to flip it

18 flashcards in this kit — the app reviews them with spaced repetition so the right card returns on the right day.

Learning games

Play your way through this kit

Every game is built from this kit's own content — scores feed your mastery, so playing counts as studying.

🎮

Word Match

Built from this kit's own content

🎮

True False

Built from this kit's own content

🎮

Memory Match

Built from this kit's own content

🎮

Flashcard Battle

Built from this kit's own content

🎮

Speed Quiz

Built from this kit's own content

🎮

Guess Term

Built from this kit's own content

🎮

Sequence Builder

Built from this kit's own content

🎮

Categorization

Built from this kit's own content

🎮

Revision Battle

Built from this kit's own content

Playable in the app

Study it properly — free, in the app

The full Veda Bites deck, complete notes, spaced-repetition flashcards, leveled MCQs, tests and games for this kit — plus Daily Facts and the Arena, every day.