| struct | Keyword

Keyword Index

Groups variables into a single record.

The syntax for defining records is:

    struct [struct-type-name]
      {
        [type variable-names] ;
        ...
      } [structure-variables] ;

A struct, like an union, groups variables into a single record. The struct-type-name is an optional tag name that refers to the structure type. The structure-variables are the data definitions, and are also optional. Though both are optional, one of the two must appear.
Elements in the record are defined by naming a type, followed by variable-names separated by commas. Different variable types can be separated by a semicolon. For example,

    struct my_struct
      {
        char name[80], phone_number[80];
        int age, height;
      } my_friend;

declares a record variable my_friend containing two strings (name and phone_number) and two integers (age and height). To declare additional variables of the same type, you use the keyword `struct` followed by the struct-type-name, followed by the variable names. For example,

    struct my_struct my_friends[100];

declares an array named my_friends which components are records. In fact, `'struct my_struct'` becomes a new type which is equal in rights with any built-in type.
To access elements in a structure, you use a record selector (`'.'`). For example,

    strcpy (my_friend.name, "Mr. Wizard");

A bit field is an element of a structure that is defined in terms of bits. Using a special type of struct definition, you can declare a structure element that can range from 1 to 16 bits in length. For example,

    struct bit_field
      {
        int bit_1 : 1;
        int bits_2_to_5 : 4;
        int bit_6 : 1;
        int bits_7_to_16 : 10;
      } bit_var;