liveBook · Manning

Struct

As explained in Modern C

A struct is a composite data type or structure that groups related fields or values into a single unit.

Suppose four names belong together, but each needs a meaningful label. A `struct birdStruct` can give them members named `jay`, `magpie`, `raven`, and `chough`; `aName.raven` then selects one member without relying on an array index. section 6.3

struct birdStruct {
  char const* jay;
  char const* magpie;
  char const* raven;
  char const* chough;
};
struct birdStruct const aName = {
  .chough = "Henry",
  .raven = "Lissy",
  .magpie = "Frau",
  .jay = "Joe",
};


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":6}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":2},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":12}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":2},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":12}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":2},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":12}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":2},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":12}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":6}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":12}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":12}],[{\"line\":6,\"ch\":18},{\"line\":6,\"ch\":23}]]"}
!@%STYLE%@!

Read the type declaration first, then the separate definition of `aName`. The initializer names each member with a leading dot, regardless of its position in the declaration. section 6.3

TAKEAWAY: A `struct` is an aggregate whose named members can have different types. Arrays instead group elements of one base type, where a first, second, and subsequent element make sense. Declaring a structure type describes that type; it does not by itself declare an object. Member declarations resemble ordinary variable declarations, and `.` selects a member of a structure object. In the bird example, the members refer to strings; the strings themselves are not part of the structure. section 6.3 section 6.0

Calendar fields

An array of calendar values leaves a question: which index holds the year? `struct tm` gives fields names such as `tm_year`, `tm_mon`, and `tm_mday`. A designated initializer sets those fields explicitly; the illustrated date uses `2019-1900` for `tm_year` and `4-1` for `tm_mon`. Code can then read `today.tm_year` and add 1900 when displaying the year. A function can receive a `struct tm`, change its `tm_yday` member, and return the resulting structure. section 6.3

Structures can also be members of other structures: a person can contain a date structure, which in turn contains calendar and precision fields. When code instead has a pointer to a structure, `->` selects a member of the pointed-to object: `a->tv_sec` means `(*a).tv_sec`. The selected member is the member object, not necessarily another pointer. section 6.3 section 11.2

How Modern C covers it

How deep it goes
Works through type declarations, designated initialization, member access, nested structures, and pointer access; defers the broader treatment of pointers.
Examples
Uses named bird fields, calendar fields in `struct tm`, and a function that updates a day-of-year field.
What it assumes
Builds on arrays and ordinary variable declarations while introducing structure members and postponing a full discussion of pointers.
What you can do after
Build it from scratch: declare a structure, initialize named members, access them with `.`, and use `->` through a structure pointer.
How it connects
Connects structures to arrays, pointers, nested structures, and typedef.

Also explained in

Compare how 14 books explain struct →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.