Next: , Previous: , Up: Top   [Contents][Index]


15 Structures

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.


Next: , Previous: , Up: Top   [Contents][Index]