28F. Summary


Copying structured values

If x and y have structure type Elephant then statement

  x = y;
does a bit-by-bit copy of the memory occupied by y into the memory occupied by x. The copy is shallow. Fields that are pointers are copied as pointers; the things that they point to are not copied.


Compound selectors

Selectors for structures and arrays can be applied to any expression that has the correct type. For example, if type Elephant is defined by

  struct Elephant
  {
    int size;
    const char* name;
  };
and A is an array of Elephants, then A[0].name is the name of the Elephant stored at index 0 in array A, and A[0].name[0] is the first character of that name.


Passing structures to functions

You almost always pass a structure by reference or by pointer.

Passing structures by value has led to many errors in programming assignment submissions.


Documentation

The standards require you to document every structure type. Include


Forward declarations of structure types

Declaration

  struct Elephant;
allows you to use type Elephant*. You cannot use type Elephant until its full definition has occurred.