Skip to main content
Logo image

Section 26.2 Arrays of Structures

We’ll now look at storing multiple structures in an array. As an example you might think of creating a record (structure) for each student in a class, consisting of the student’s name, year of birth and GPA. You could then store the information for all students in a class in an array with each array elements being a student structure. You’d thus have an array of structures.
Let’s practice! Rather than using student data, in today’s class we’d like to store the vertices of a polygon in an array of structures. Each vertex consists of an x- and a y-coordinate. In order to efficiently store these coordinates for a point we’ll define a new structure, called point_t, that stores these coordinates for a given point. The code window below already has such a structure defined.

Activity 26.1.

Let’s warm up by quickly writing some functions that serve to assign values to a variable of type point_t and print the coordinates of a variable of type point_t.
More specifically:
  • write a function initializePoint() to which you pass a variable of type point_t (by reference) along with two floats, and that assigns the first float to the x-coordinate and the second float to the y-coordinate of the point.
  • write a function printPoint() to which you pass a variable of type point_t (by value) and that prints out the coordinates of the variable in the form (x,y).

admin.....open in new window

When you are done, please paste your code into the code submission box below: