Next: Unions, Previous: Overlaying 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! */