← Structs and UnionsC PROGRAMMING 1.4.A

Struct Basics

Declaration, member access, initialization.

StatusCollected
NotesChapter test: 5/5

Struct basics: bundling different types together under one name, dot access, and initialization.

struct point {
    int x;
    int y;
};

struct point p1 = {10, 20};
struct point p2 = {.x = 5, .y = 7};   /* designated initializers — order-independent */

printf("%d\n", p1.x);

A struct is just a labeled bundle of memory — unlike an array, the members don't have to be the same type, and each one gets accessed by name with . instead of by index. The designated-initializer form (.x = 5) is the one I actually reach for now, since it doesn't depend on remembering member order.

Notes from readers

Comments — via GitHub