C Reference ยท Keywords and Statements
The struct keyword
Reference for C struct: declaring members, initialization, member access with . and ->, copying, nesting, self-reference and padding rules.
A struct groups named members of possibly different types into one object. Members are stored in the order they are declared, so a struct is the basic building block for records, nodes, vectors, and almost every data structure in C.
Structs are values: you can assign one to another, pass it to a function by value, and return it. Assignment copies every member (including arrays inside the struct). Use . to reach a member of a struct object and -> to reach a member through a pointer.
struct point { int x; int y; }; struct point p = {.x = 3, .y = 4};
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
struct tag { member-declarations }; /* define a type */ struct tag object; /* declare an object */ struct tag { ... } object1, *object2; /* define and declare */ struct { ... } object; /* anonymous struct type */
| Part | Description |
|---|---|
tag |
Optional name. The full type name is struct tag; tags have their own name space in C. |
| members | Declarations like int x;. Bit-fields (unsigned flags : 3;) and one flexible array member (last) are allowed. |
| initializer | {1, 2} by position, {.y = 2} by name (designated), {} or for zero-initialization. |
Returns: sizeof(struct tag) is at least the sum of member sizes; padding may be added.
Errors / undefined behavior: a struct must be complete to be instantiated; only a pointer to the struct itself can be a member. Comparing structs with == does not compile. Using memcmp on structs can compare padding bytes and give wrong answers.
Usage notes
- Layout. Members appear in declaration order with no reordering. The compiler inserts padding to satisfy alignment; reorder members from largest to smallest alignment to reduce size.
- Assignment copies deeply only for arrays and values. A pointer member is copied as a pointer, so both copies point to the same object.
- Unnamed members. Anonymous structs and unions are allowed inside a struct (C11) and expose their members directly.
- Incomplete types.
struct tag;alone declares a type that you can use through pointers only, the basis for opaque handles. - Zero-initialization.
works in all versions;{}is standard from C23 (GCC accepted it earlier as an extension). - Passing by value copies the whole struct. For large structs pass a
const struct tag *. - Same-type compatibility. Two separate struct definitions with identical members are different types, except across translation units where C allows compatible declarations.
- typedef often removes the need to write
structeach time:typedef struct point point;.
Related
typedef,union,sizeof, bit-fields, padding- Lessons: Structures, Structs and pointers