Structures

A structure contains an ordered group of data objects. Unlike the elements of an array, the data objects within a structure can have varied data types. Each data object in a structure is a member or field.

C A member of a structure may have any object type other than a variably modified type. Every member except the last must be a complete type. As a special case, the last element of a structure with more than one member may have an incomplete array type, which is called a flexible array member.

C++ In C++, a structure member must be a complete type.

Use structures to group logically related objects. For example, to allocate storage for the components of one address, define the following variables:

   int street_no;
   char *street_name;
   char *city;
   char *prov;
   char *postal_code;

To allocate storage for more than one address, group the components of each address by defining a structure data type and as many variables as you need to have the structure data type.

C++ In C++, a structure is the same as a class except that its members and inheritance are public by default.

In the following example, line int street_no; through to char *postal_code; declare the structure tag address:

      struct address {
                       int street_no;
                       char *street_name;
                       char *city;
                       char *prov;
                       char *postal_code;
                     };
      struct address perm_address;
      struct address temp_address;
      struct address *p_perm_address = &perm_address;

The variables perm_address and temp_address are instances of the structure data type address. Both contain the members described in the declaration of address. The pointer p_perm_address points to a structure of address and is initialized to point to perm_address.

Refer to a member of a structure by specifying the structure variable name with the dot operator (.) or a pointer with the arrow operator (->) and the member name. For example, both of the following:

perm_address.prov = "Ontario";
p_perm_address -> prov = "Ontario";

assign a pointer to the string "Ontario" to the pointer prov that is in the structure perm_address.

All references to structures must be fully qualified. In the example, you cannot reference the fourth field by prov alone. You must reference this field by perm_address.prov.

Structures with identical members but different names are not compatible and cannot be assigned to each other.

Structures are not intended to conserve storage. If you need direct control of byte mapping, use pointers.

Compatible Structures

C Each structure definition creates a new structure type that is neither the same as nor compatible with any other structure type in the same source file. However, a type specifier that is a reference to a previously defined structure type is the same type. The structure tag associates the reference with the definition, and effectively acts as the type name. To illustrate this, only the types of structures j and k are the same.

struct   { int a; int b; } h;
struct   { int a; int b; } i;
struct S { int a; int b; } j;
struct S k;

Related References

IBM Copyright 2003