Structs in memory Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

Define a type, then make variables of it

struct Name { members }; defines a new type — a blueprint that lists the members and their types. It uses no memory by itself. Then struct Name v; creates a variable with all those members side by side. Reach a member with the dot: v.member. Structs can be initialised with { }, copied with =, put in arrays and nested inside other structs.

struct Point {          // the type (no memory yet)
    int x;
    int y;
};                      // note the semicolon!

struct Point p = {3, 4};   // a variable: 8 bytes
p.x = 10;                  // one member
struct Point q = p;        // copies both members
struct Point pts[10];      // an array of 10 points
struct S { char c; int i; char d; } — where the members go
Member Size Offset Why
char c 1 0 The first member starts at offset 0
(padding) 3 1–3 int must start at a multiple of 4
int i 4 4
char d 1 8
(padding) 3 9–11 The size is rounded up to a multiple of 4
sizeof(struct S) 12 — Reordered as int, char, char it is only 8

Defining and creating

  1. struct Student { char name[20]; int age; double gpa; }; — end with a semicolon.
  2. The definition usually goes above main, so every function can use the type.
  3. struct Student s; creates one variable; its members are garbage until set.
  4. Initialise in order: struct Student s = {"Ann", 19, 8.5};
  5. Or by name (C99): struct Student s = {.age = 19, .name = "Ann"};

Using members

  1. s.age = 20; — the dot selects one member.
  2. A member is an ordinary variable: s.age++, scanf("%d", &s.age).
  3. Text members are char arrays: use strcpy(s.name, "Bob"), not =.
  4. Whole structs can be assigned: t = s; copies every member, arrays too.
  5. Structs cannot be compared with == — compare the members one by one.

Arrays, nesting and size

  1. struct Student cls[30]; — cls[i].age is the age of student i.
  2. A member can be another struct: b.published.year.
  3. Members are placed in order, each at a multiple of its own size (alignment).
  4. The gaps are padding; the total is rounded up too.
  5. Ordering members from largest to smallest often saves memory. Always use sizeof.
Walkthrough

Books with a nested date

Every line, explained.

1 #include <stdio.h>
2 #include <string.h>
3
4 struct Date {
5 int day, month, year;
6 };
7
8 struct Book {
9 char title[30];
10 double price;
11 struct Date published; A struct inside a struct.
12 };
13
14 int main(void)
15 {
16 struct Book b = {"C Basics", 299.50, {15, 8, 2024}}; Nested braces for the nested struct.
17 struct Book shelf[2]; An array of structs.
18 shelf[0] = b; Copies the whole book, title included.
19 strcpy(shelf[1].title, "Pointers"); A char array member needs strcpy.
20 shelf[1].price = 349.00;
21 shelf[1].published.day = 1; Two dots: book → date → day.
22 shelf[1].published.month = 1;
23 shelf[1].published.year = 2025;
24 for (int i = 0; i < 2; i++)
25 printf("%s %.2f %d\n", shelf[i].title, shelf[i].price, shelf[i].published.year); C Basics 299.50 2024 / Pointers 349.00 2025
26 printf("%zu bytes per book\n", sizeof(struct Book)); 30 + 2 padding + 8 + 12 = 52 → rounded to 56
27 return 0;
28 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Missing semicolon after the definition

✗ Wrong
struct Point { int x; int y; }
int main(void) { … }
✓ Right
struct Point { int x; int y; };
int main(void) { … }

Without ; the compiler reads the next line as part of the struct declaration — confusing errors follow.

Forgetting the word struct

✗ Wrong
Point p;            // error in C
✓ Right
struct Point p;     // or use typedef (Module 35)

In C the type's full name is "struct Point".

Assigning text with =

✗ Wrong
s.name = "Ann";
✓ Right
strcpy(s.name, "Ann");

name is a char array, and arrays cannot be assigned. (Copying the whole struct with = is fine.)

Comparing structs with ==

✗ Wrong
if (p == q) …
✓ Right
if (p.x == q.x && p.y == q.y) …

C has no == for structs; compare the members.

Assuming sizeof is the sum of the members

✗ Wrong
// char + int + char = 6 bytes?
✓ Right
printf("%zu", sizeof(struct S));   // 12

Padding is added for alignment. Never hard-code a struct's size.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

struct P { int x; int y; }; struct P a = {1, 2}; — what is a.y?

Question 2

struct P a = {1, 2}; struct P b = a; b.x = 9; — what is a.x?

Question 3

Which operator selects a member of a struct variable?

Question 4

struct S { char c; int i; }; — what is sizeof(struct S) on a typical system?

Question 5

struct Student cls[30]; — how do you get the age of the 5th student?

Assessment

Structures quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.