Aral Code

Memory Lesson 13 of 16

C Structures (struct) and typedef

Group related data into a struct, create arrays of structs, use typedef for cleaner names, access members with . and ->, and pass structs to functions.

A student has a name, a grade level and an average. Keeping those in three separate arrays is messy. A struct (structure) bundles related values of different types into one new type.

Defining and using a struct

#include <stdio.h>

struct Student {
    char name[40];
    int grade_level;
    double average;
};

int main(void) {
    struct Student s = {"Maria Clara", 11, 94.25};
    printf("%s, Grade %d, average %.2f\n", s.name, s.grade_level, s.average);

    s.average = 95.0;        /* change a member */
    printf("New average: %.2f\n", s.average);
    return 0;
}

Output

Maria Clara, Grade 11, average 94.25
New average: 95.00

The struct definition is a blueprint; s is a variable of that type. Each part (name, grade_level, average) is a member, accessed with a dot.

typedef: shorter type names

typedef struct {
    char name[30];
    double price;
    int stock;
} Product;

Product p = {"Kape 3-in-1", 8.0, 200};   /* no need to write "struct" */

Arrays of structs

#include <stdio.h>

typedef struct {
    char name[30];
    double price;
    int stock;
} Product;

int main(void) {
    Product items[] = {
        {"Pandesal (10 pcs)", 25.0, 40},
        {"Kape 3-in-1", 8.0, 200},
        {"Itlog", 9.0, 0},
        {"Instant Noodles", 15.0, 120}
    };
    int n = sizeof items / sizeof items[0];

    printf("%-20s %8s %6s\n", "ITEM", "PRICE", "STOCK");
    for (int i = 0; i < n; i++) {
        printf("%-20s %8.2f %6d%s\n", items[i].name, items[i].price, items[i].stock,
               items[i].stock == 0 ? "  <- OUT" : "");
    }
    return 0;
}

Output

ITEM                    PRICE  STOCK
Pandesal (10 pcs)       25.00     40
Kape 3-in-1              8.00    200
Itlog                    9.00      0  <- OUT
Instant Noodles         15.00    120

Each element of the array is a whole product record. This is much cleaner than parallel arrays.

Structs and functions

Structs are passed by value (copied), so a function can't change the original unless you pass a pointer. With a pointer, use the arrow -> to reach members:

#include <stdio.h>

typedef struct {
    char owner[30];
    double balance;
} Account;

void print_account(Account a) {               /* copy: read only */
    printf("%s: P%.2f\n", a.owner, a.balance);
}

int withdraw(Account *a, double amount) {     /* pointer: can change */
    if (amount > a->balance) return 0;
    a->balance -= amount;                     /* same as (*a).balance */
    return 1;
}

int main(void) {
    Account acct = {"Juan", 1000.0};
    if (!withdraw(&acct, 1500)) printf("Insufficient balance.\n");
    withdraw(&acct, 250);
    print_account(acct);
    return 0;
}

Output

Insufficient balance.
Juan: P750.00

a->balance is shorthand for (*a).balance: "follow the pointer, then get the member".

Nested structs

#include <stdio.h>

typedef struct { int year, month, day; } Date;
typedef struct {
    char title[40];
    Date due;
} Assignment;

int main(void) {
    Assignment a = {"Research paper", {2026, 12, 15}};
    printf("%s due %04d-%02d-%02d\n", a.title, a.due.year, a.due.month, a.due.day);
    return 0;
}

Output

Research paper due 2026-12-15

Finding the top student

#include <stdio.h>

typedef struct {
    char name[20];
    double average;
} Student;

int main(void) {
    Student cls[] = {{"Ana", 91.5}, {"Ben", 84.0}, {"Carla", 95.2}, {"Dan", 88.7}};
    int n = 4, best = 0;
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += cls[i].average;
        if (cls[i].average > cls[best].average) best = i;
    }
    printf("Top student: %s (%.1f)\n", cls[best].name, cls[best].average);
    printf("Class average: %.2f\n", sum / n);
    return 0;
}

Output

Top student: Carla (95.2)
Class average: 89.85

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you access the price member of a struct variable p?
2. If ptr is a pointer to a struct, how do you access price?
3. What does typedef do?
4. How is a struct passed to a function by default?

By Sir Reyel · Last updated 2026-12-04