A structure is a user-defined data type that holds various fields of data. Each field has a name and a data type specified in the structure’s definition.
Here we define a structure suitable for storing a linked list of integers. Each list item will hold one integer, plus a pointer to the next item.
struct intlistlink { int datum; struct intlistlink *next; };
The structure definition has a type tag so that the code can
refer to this structure. The type tag here is intlistlink
.
The definition refers recursively to the same structure through that
tag.
You can define a structure without a type tag, but then you can’t
refer to it again. That is useful only in some special contexts, such
as inside a typedef
or a union
.
The contents of the structure are specified by the field declarations inside the braces. Each field in the structure needs a declaration there. The fields in one structure definition must have distinct names, but these names do not conflict with any other names in the program.
A field declaration looks just like a variable declaration. You can combine field declarations with the same beginning, just as you can combine variable declarations.
This structure has two fields. One, named datum
, has type
int
and will hold one integer in the list. The other, named
next
, is a pointer to another struct intlistlink
which would be the rest of the list. In the last list item, it would
be NULL
.
This structure definition is recursive, since the type of the
next
field refers to the structure type. Such recursion is not
a problem; in fact, you can use the type struct intlistlink *
before the definition of the type struct intlistlink
itself.
That works because pointers to all kinds of structures really look the
same at the machine level.
After defining the structure, you can declare a variable of type
struct intlistlink
like this:
struct intlistlink foo;
The structure definition itself can serve as the beginning of a variable declaration, so you can declare variables immediately after, like this:
struct intlistlink { int datum; struct intlistlink *next; } foo;
But that is ugly. It is almost always clearer to separate the definition of the structure from its uses.
Declaring a structure type inside a block (see Blocks) limits the scope of the structure type name to that block. That means the structure type is recognized only within that block. Declaring it in a function parameter list, as here,
int f (struct foo {int a, b} parm);
(assuming that struct foo
is not already defined) limits the
scope of the structure type struct foo
to that parameter list;
that is basically useless, so it triggers a warning.
Standard C requires at least one field in a structure. GNU C does not require this.
• Referencing Fields | Accessing field values in a structure object. | |
• Dynamic Memory Allocation | Allocating space for objects while the program is running. | |
• Field Offset | Memory layout of fields within a structure. | |
• Structure Layout | Planning the memory layout of fields. | |
• Packed Structures | Packing structure fields as close as possible. | |
• Bit Fields | Dividing integer fields into fields with fewer bits. | |
• Bit Field Packing | How bit fields pack together in integers. | |
• const Fields | Making structure fields immutable. | |
• Zero Length | Zero-length array as a variable-length object. | |
• Flexible Array Fields | Another approach to variable-length objects. | |
• Overlaying Structures | Casting one structure type over an object of another structure type. | |
• Structure Assignment | Assigning values to structure objects. | |
• Unions | Viewing the same object in different types. | |
• Packing With Unions | Using a union type to pack various types into the same memory space. | |
• Cast to Union | Casting a value one of the union’s alternative types to the type of the union itself. | |
• Structure Constructors | Building new structure objects. | |
• Unnamed Types as Fields | Fields’ types do not always need names. | |
• Incomplete Types | Types which have not been fully defined. | |
• Intertwined Incomplete Types | Defining mutually-recursive structue types. | |
• Type Tags | Scope of structure and union type tags. |