liveBook · Manning

Struct

As explained in Modern C, Third Edition

The struct keyword introduces or refers to a C structure type through its structure tag.

The struct keyword introduces or refers to a structure type through its structure tag. A structure combines named members that may have different types, unlike an array, whose elements share one base type. section 6.0 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",
};

This declaration defines struct birdStruct with four named pointer members; the later definition initializes an object and accesses members with the dot operator. section 6.3.1

Use structures to make data organization explicit:

  • Declare the structure type separately from objects of that type; defining a struct type does not itself define an object. section 6.3.1
  • Designate members by name with the dot operator, avoiding the ambiguity of assigning meaning to array indices. section 6.3.1
  • Group unlike types, such as whole seconds and nanoseconds in struct timespec. section 6.3.2
  • Nest structures when a member is itself a structure; nested declarations share the surrounding declaration's visibility rules. section 6.3.3

Representing a timestamp

A struct tm object can name calendar fields such as tm_year, tm_mon, and tm_mday. A function can accept a struct tm value, update tm_yday while accounting for leap years, and return the updated structure. section 6.3.1

How Modern C, Third Edition covers it

How deep it goes
Explains declarations, objects, member access, heterogeneous fields, nesting, and visibility, while deferring detailed layout and padding treatment.
Examples
Uses bird records, calendar timestamps, nanosecond timestamps, nested person data, and a timespec difference exercise.
What it assumes
Assumes basic C types and arrays, then develops named aggregation before deferring deeper pointer and memory-model details.
What you can do after
Apply C structure declarations, designated initializers, dot notation, heterogeneous fields, and nested member organization.
How it connects
Connects structures with arrays, pointers, enumerations, typedefs, unions, nested structures, and standard time types.

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, Third Edition. The section links point to the parts of the book it draws on.