UMBC CS 201, Fall 05
Arrays of Structures
It should come to as no surprise that since we can create variables
which are structures, we can also create arrays of structures, just
like we create arrays of int, float, or some other data type.
Recall that declaring an array of ints
allocates contiguous memory for the integers and uses the name of the
array as a pointer to the array.
We can then access the elements of the array using subscripts. Each of
the elements is an int.
grades = 90;
Similarly, we can declare an array of a particular type of structure.
We can declare an array of 'struct point's as
struct point points;
or, using the typefdef alias POINT, we can equivalently write
In an array of ints, each element (like grades) is an int.
So too, in an array of 'struct point's, each element (like points) is a
We reference the members of the structures in the array using
the dot operator.
points.x is the x-coordinate of the 4th element of the
points.y is the y-coordinate of the 1st element of the array.
We can assign array elements to them as in this code fragment
/* assuming the array elements are initialized */
/* assign (copy) the 11th element into point1 */
point1 = points;
/* print the x-coordinate of the 2nd element */
printf ("%d", points.x);
/* print the y-coordinate of the 2nd element */
printf ("%d", points.y);
Sunday, 25-Sep-2005 10:38:56 EDT