15.2: Accessing Members of Structures

We said that a structure was a little bit like an array: a collection of members (elements). We access the elements of an array by using a numeric subscript in square brackets []. We access the elements of a structure by name, using the structure selection operator which is a dot (a period). The structure selection operator is a little like the other binary operators we've seen, but much more restricted: on its left must be a variable or object of structure type, and on its right must be the name of one of the members of that structure. For example, if c1 is a variable of type struct complex as declared in the previous section, then c1.real is its real part and c1.imag is its imaginary part.

Like subscripted array references, references to the members of structure variables (using the structure selection operator) can appear anywhere, either on the right or left side of assignment operators. We could say

	c1.real = 1
to set the real part of c1 (that is, the real member within c1) to 1, or
	c1.imag = c2.imag
to fetch the imaginary part of c2 and assign it to the imaginary part of c1, or
	c1.real = c2.real + c3.real
to take the real parts of c2 and c3, add them together, and assign the result to the real part of c1.

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback