Structures and Unions in C: 22 flashcards

All 22 flashcards from this kit, free and with no sign-up.

1. What is a `struct` in C?

Show the answer

A `struct` is a user-defined data type that groups variables of possibly different types into a single named type. All members exist simultaneously, each with its own memory.

2. What is a `union` in C?

Show the answer

A `union` is a user-defined data type that allows storing different types in the same memory location. Only one member is active at a time, and all members share the same starting address.

3. How does memory allocation differ between a `struct` and a `union`?

Show the answer

A `struct` allocates memory equal to the sum of all its members (plus padding for alignment). A `union` allocates memory equal to the size of its largest member.

4. Given `struct S { int a; char b; };` and `union U { int a; char b; };`, what are the approximate sizes?

Show the answer

`struct S` is about 8 bytes (4 for int + 1 for char + 3 padding). `union U` is 4 bytes (size of the largest member, int).

5. In a `struct`, can you access all members at the same time?

Show the answer

Yes, in a `struct`, all members are valid simultaneously. You can read or write any member independently without affecting others.

6. In a `union`, what happens when you write to one member?

Show the answer

Writing to one member overwrites the others because all members share the same memory location. Only the last written member is valid.

7. What is the primary use case for a `struct`?

Show the answer

Use a `struct` when you need to group related but distinct data that you access together, like a student record with id, name, and GPA.

8. What is the primary use case for a `union`?

Show the answer

Use a `union` when you have a variable that can hold one of several types but never more than one at a time, like a token in a parser that could be a number, string, or operator.

9. Provide a practical analogy for a `struct`.

Show the answer

A `struct` is like a toolbox with separate compartments — every tool has its own slot, and all tools are available at once.

10. Provide a practical analogy for a `union`.

Show the answer

A `union` is like a single drawer where you put one tool at a time — put in a hammer, the wrench is gone.

11. What is the size of the following union? `union Value { int i; float f; char str[20]; };`

Show the answer

20 bytes, because the largest member is `char str[20]`.

12. What is the size of the following struct? `struct Student { int id; char name[50]; float gpa; };`

Show the answer

At least 58 bytes (4 + 50 + 4), plus possible padding for alignment.

13. In memory, how are `struct` members laid out?

Show the answer

Struct members are laid out sequentially, each in its own memory block, with possible padding between members for alignment.

14. In memory, how are `union` members laid out?

Show the answer

All union members overlap at the same starting address. They share the same memory space.

15. When would you choose a `union` over a `struct`?

Show the answer

Choose a `union` when you need to save memory and only one of several types is needed at a time, such as in a variant or a network packet header.

16. When would you choose a `struct` over a `union`?

Show the answer

Choose a `struct` when you need to keep all fields together and access them simultaneously, such as a date with day, month, and year.

17. What is a tagged union?

Show the answer

A tagged union is a `struct` that contains a `union` along with a type indicator (tag) to know which member is currently active. It's used in compilers or network packets.

18. Can you read a `union` member that was not the last one written?

Show the answer

Technically, you can read it, but the value is undefined or garbage because the memory has been overwritten by the last write. Only the last written member is valid.

19. What is the key difference in member access between `struct` and `union`?

Show the answer

In a `struct`, all members are valid and accessible at any time. In a `union`, only one member is valid at a time; writing to one invalidates others.

20. Given `union U { int i; float f; };` and you write `u.i = 42;` then `u.f = 3.14;`, what is the value of `u.i`?

Show the answer

The value of `u.i` is undefined because writing to `u.f` overwrote the memory. Only `u.f` is valid.

21. What is the purpose of padding in a `struct`?

Show the answer

Padding is added to align members to memory boundaries for efficient access. It increases the struct's size but improves performance.

22. How can you access a member of a `struct` or `union` using a pointer?

Show the answer

Use the arrow operator `->` when you have a pointer to the struct or union, e.g., `ptr->member`. Use the dot operator `.` when you have the variable itself.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages