Skip to main content

Lesson 3 of 5

typedef & Nested Structures

`typedef` creates an alias for a type. For structs, it eliminates the need to write `struct Foo` everywhere. Nested structs embed one struct inside another - the inner struct's members are accessed with chained `.` or `->`. Self-referential structs (for linked lists, trees) require a forward declaration with the struct tag.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

typedef, Nested Structs & Self-Referential Structs

typedef, Nested Structs & Self-Referential Structs
#include <stdio.h>

/* ── typedef struct - clean alias ───────────────────────── */
typedef struct {
    float x, y;
} Vec2;

typedef struct {
    float x, y, z;
} Vec3;

/* ── Nested struct ───────────────────────────────────────── */
typedef struct {
    Vec2   position;   /* embedded, not a pointer */
    Vec2   velocity;
    float  mass;
    char   name[32];
} Particle;

/* ── Self-referential struct - requires tag, not just typedef */
typedef struct Node {       /* struct tag 'Node' needed here  */
    int          data;
    struct Node *next;      /* pointer to same struct type     */
} Node;

void print_particle(const Particle *p) {
    printf("%-10s pos=(%.1f,%.1f) vel=(%.1f,%.1f) mass=%.2f
",
           p->name,
           p->position.x, p->position.y,
           p->velocity.x, p->velocity.y,
           p->mass);
}

Vec2 vec2_add(Vec2 a, Vec2 b) {
    return (Vec2){ a.x + b.x, a.y + b.y };   /* compound literal (C99) */
}

int main(void) {
    Vec2 v1 = {1.0f, 2.0f};
    Vec2 v2 = {3.0f, 4.0f};
    Vec2 sum = vec2_add(v1, v2);
    printf("sum = (%.1f, %.1f)
", sum.x, sum.y);  /* (4.0, 6.0) */

    /* ── Nested struct access ────────────────────────────── */
    Particle p = {
        .name     = "proton",
        .position = {0.0f, 0.0f},
        .velocity = {1.5f, -0.5f},
        .mass     = 1.67e-27f
    };
    print_particle(&p);
    p.position.x += p.velocity.x;   /* chained dot access     */
    p.position.y += p.velocity.y;
    print_particle(&p);

    /* ── Self-referential: building a mini linked list ───── */
    Node n3 = {30, NULL};
    Node n2 = {20, &n3};
    Node n1 = {10, &n2};
    /* list: n1 -> n2 -> n3 -> NULL */
    for (Node *cur = &n1; cur != NULL; cur = cur->next) {
        printf("%d ", cur->data);
    }
    printf("
");

    return 0;
}

Common Mistakes

  • Self-referential struct using typedef but no tag - `typedef struct { int d; MyNode *next; } MyNode;` - the typedef `MyNode` is not complete when the struct body is being parsed. Use a struct tag: `typedef struct MyNode { int d; struct MyNode *next; } MyNode;`.
  • Forgetting that nested structs are embedded by value - `Particle p; p.position` is a `Vec2` directly inside `p`. `&p.position` gives a legitimate pointer. This is not the same as `Particle` having a `Vec2*` pointer member.
  • Using `->` on an non-pointer struct - `Particle p; p->name` is a compile error. `->` is ONLY for pointers to structs.

Tip

Tip

Practice typedef Nested Structures in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

typedef creates clean type aliases and self-referential struct pointers enable linked structures

Practice Task

Note

Practice Task - (1) Write a working example of typedef Nested Structures from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with typedef Nested Structures is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • `typedef` creates an alias for a type.
  • Self-referential struct using typedef but no tag - `typedef struct { int d; MyNode *next; } MyNode;` - the typedef `MyNode` is not complete when the struct body is being parsed. Use a struct tag: `typedef struct MyNode { int d; struct MyNode *next; } MyNode;`.
  • Forgetting that nested structs are embedded by value - `Particle p; p.position` is a `Vec2` directly inside `p`. `&p.position` gives a legitimate pointer. This is not the same as `Particle` having a `Vec2*` pointer member.
  • Using `->` on an non-pointer struct - `Particle p; p->name` is a compile error. `->` is ONLY for pointers to structs.

Topics in This Module

0/5 done