Difference between structure and union

Structure vs union in one line: a struct gives every member its OWN memory (size = sum) so all live together; a union makes members SHARE one block (size = largest) so only the last-written one is alive.

9 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

One keyword, two philosophies

Last lesson ended with a puzzle: struct and union share IDENTICAL syntax, braces, members, dots, yet behave like different species.

Exams adore this pair: "Differentiate between structure and union" appears so reliably that seniors call it free marks.

Today: the comparison table, the sizeof proof, and the one sentence that untangles the two forever. The sentence involves a hostel.

Theory

Separate rooms vs one shared room

A struct is a hostel wing: every member gets their OWN room, all can live there at the SAME time, and the wing's size is the SUM of the rooms. A union is a single room with a rotating nameplate: whoever moves in throws the previous occupant's luggage out. Its size? Just big enough for the LARGEST possible occupant. Same building form; opposite housing policy.

At a glance

The comparison exams demand

AspectStructureUnion
MemoryEach member has own spaceAll members share ONE block
SizeSum of members (+ padding)Largest member only
Alive at onceAll members simultaneouslyOnly the last-written member
Reading othersAlways validGarbage (overwritten bytes)
Best forRecords: student, employeeMemory-scarce, one-of-many values

Practical

The sizeof proof

#include <stdio.h>

struct sBox { int i; float f; char c; };
union  uBox { int i; float f; char c; };

int main() {
    printf("struct: %lu bytes\n", sizeof(struct sBox));
    printf("union : %lu bytes\n", sizeof(union uBox));
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Predict the two sizes

Same three members: int (4), float (4), char (1). Reason out both sizeof results before revealing. (Hint: sum for one, largest for the other, and C may pad.)

Show the answer

struct: 4 + 4 + 1 = 9, padded by most compilers to 12 (members aligned to 4-byte boundaries). union: the largest member is 4 bytes, so 4.

Write "sum of members, possibly padded" vs "size of largest member" and the sizeof question is fully earned. The 12-vs-4 gap IS the difference, in numbers.

Quiz

When is a UNION the right choice over a struct?

  1. When a variable holds only ONE of several types at a time and memory is precious
  2. Whenever you want faster access to members
  3. When all members must hold values together
  4. Never: unions are obsolete
Show the answer

When a variable holds only ONE of several types at a time and memory is precious

The union's one talent is thrift: a sensor reading that is EITHER an int OR a float (never both) needs only the larger space, not the sum, gold in embedded systems and device programming. Needing members TOGETHER (option C) is precisely the struct's job. Choice questions here test exactly this either/or judgement.

Quiz

struct sBox s; s.i = 10; s.f = 2.5; printf("%d", s.i); versus the same sequence on union uBox u. What prints in each case?

  1. struct prints 10; union prints garbage
  2. Both print 10
  3. Both print garbage
  4. struct prints garbage; union prints 10
Show the answer

struct prints 10; union prints garbage

In the struct, i and f live in SEPARATE rooms: writing f cannot disturb i, so 10 survives. In the union they are one room: u.f = 2.5 evicted the 10, and reading u.i reinterprets float bytes, garbage. This paired trace is the cleanest possible answer to "demonstrate the difference with an example".

Watch out

Where marks leak

Writing exact struct sizes without mentioning padding: say "sum of members, plus possible alignment padding", the honest full-mark phrasing. Claiming unions are faster: their virtue is SIZE, not speed. Initializing a union with multiple values: only the FIRST member can be initialized ({10} works, {10, 2.5} does not). And in the table answer, always include the use-case row, that is where the fifth mark hides.

Theory

The subject, complete

Stand back and look: the marks program began printing one hard-coded line, and ends as an array of student records it can read, grade, sort and report, using every construct in the syllabus. That program IS your revision: rebuild it from memory before the exam, and each unit re-teaches itself. Next semester, Python (BCA204) will make you smile at how much you already understand.

Summary

Key takeaways

  • Struct: separate memory per member; size = sum (+ padding); all alive together.
  • Union: one shared block; size = largest member; only the last-written member is valid.
  • sizeof proof: {int, float, char} → struct ~12 (padded), union 4.
  • Union initialization touches only the first member.
  • Choose struct for records (default); union for one-of-many values under memory pressure.
  • Memory hook: separate rooms vs one room with a rotating nameplate.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Difference between structure and union · Computer Programming and Programming Methodology (CPPM) · Gri-Learn