5.13.1. Structures


Structured values

A structured value is a collection of variables, called fields, each with a name and a type. For example, a structured value might contain an integer called item and a char* pointer called name.

Structured values (or simply structures) are useful because they allow you to group values and to treat the group as a single value.


Structure types

Each structured value has a type that must be defined within the program. Define a type of structures using struct. For example,

  struct PairOfInts
  {
    int a;
    int b;
  };
creates a new type PairOfInts, where a value of type PairOfInts has two fields, called a and b, each of type int. Type definition
  struct Cell
  {
    int   item;
    char* name;
  };
defines type Cell with two fields.

Notice the semicolon at the end of a structure type definition. Be sure to include it.


Choosing field names

In this section some illustrations use single-letter names to keep things short. But field names tend to be used over a most of a program, and they should be descriptive. If a field is an edge, call it anEdge rather than e. You can probably choose an even better name than that.


Where to define structure types

If a module exports a structure type, put the type definition in a header file. If the module does not export the structure type, put the type definition near the beginning of the .cpp file. The type needs to be defined before you use it.


Documenting structure types

Each structure type must be documented. Say what an object of the given type represents. Also say what property of the object each variable in the structure represents.


Creating structured values

Create a structured value the way you would create any variable. For example, statements

  PairOfInts p;
  Cell c;
create a variable p of type PairOfInts and a variable c of type Cell. Use p.a and p.b to refer to the a and b fields of structured value p. For example,
  PairOfInts p;
  p.a = 100;
  p.b = 200;
creates and initializes a structured value.

Variables p and c created above are stored in the frame of the current function. To create a structured value in the heap, use new. For example,

  PairOfInts* q = new PairOfInts;
makes q point to a newly allocated PairOfInts structure.


Structure assignment

A structure is treated like a value that occupies a given number of bytes. For example, assuming that an int occupies 4 bytes, a value of type PairOfInts occupies 8 bytes, enough room for two ints.

You can move structures around like other values. For example,

  PairOfInts x, y;
  x.a = 20;
  x.b = 40;
  y = x;
initializes structure x, then copies that entire structure into variable y. So you will find that y.a has been set to 20 and y.b has been set to 40.


Exercises

  1. Write a definition of structure type Employee, where a value of type Employee has three fields: (1) a constant null-terminated string called name, (2) a value of type double called salary and (3) a constant null-terminated string called office. Answer

  2. Using the type Employee from the preceding exercise, write statements that create an Employee called Phil and that sets Phil's name to "Phil", salary to 80,000 and office to "232D". Answer

  3. Write statements that create another Employee variable PhilCpy, and copy all of the information from variable Phil into variable PhilCpy. Answer