Next: Unions, Previous: Overlaying Different Structures, Up: Structures [Contents][Index]
Assignment operating on a structure type copies the structure. The left and right operands must have the same type. Here is an example:
#include <stddef.h> /* DefinesNULL
. */ #include <stdlib.h> /* Declaresmalloc
. */ … struct point { double x, y; }; struct point * copy_point (struct point point) { struct point *p = (struct point *) malloc (sizeof (struct point)); if (p == NULL) fatal ("Out of memory"); *p = point; return p; }
Notionally, assignment on a structure type works by copying each of
the fields. Thus, if any of the fields has the const
qualifier, that structure type does not allow assignment:
struct point { const double x, y; };
struct point a, b;
a = b; /* Error! */
When a structure type has a field which is an array, as here,
struct record { char *name; int data[4]; }; struct record r1, r2;
structure assigment such as r1 = r2
copies array fields’
contents just as it copies all the other fields.
This is the only way in C that you can operate on the whole contents
of a array with one operation: when the array is contained in a
struct
. You can’t copy the contents of the data
field
as an array, because
r1.data = r2.data;
would convert the array objects (as always) to pointers to the zeroth
elements of the arrays (of type struct record *
), and the
assignment would be invalid because the left operand is not an lvalue.
Next: Unions, Previous: Overlaying Different Structures, Up: Structures [Contents][Index]