LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

How do structs work in C?

A struct bundles several named fields into one composite value; you can define it anonymously, as a named struct, or wrap it in a typedef for a clean type name.

Three ways to define:

// 1. Anonymous struct with variable
struct {
    char name[32];
    char age;
    short sex;
    int phonenumber;
} person;

// 2. Named struct
struct person {
    char name[32];
    char age;
};
// Need 'struct' keyword
struct person p;

// 3. typedef (most common)
typedef struct _person {
    char name[32];
    int age;
} person;
// No 'struct' keyword needed
person p;

Accessing members:

person p;
// Dot notation for variables
p.age = 25;

person *ptr = &p;
// Arrow notation for pointers
ptr->age = 25;
// Equivalent but uglier
(*ptr).age = 25;

Self-referential structs (linked lists):

typedef struct _node {
    int value;
    // Pointer to same type
    struct _node *next;
} node;

Go deeper:

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026