Skip to main content
Logo image

Section 26.4 Dynamic Arrays of Structures

Why stop at triangles? Suppose we wanted to store polygons with variable numbers of vertices.
Recall that in order to reserve space in memory (for example for an array whose size only becomes known at run-time of your program) we use the function malloc() whose prototype is
void * malloc(size_t size);
Here, size is the total amount of space you need for your data. For example, if you wanted to store 10 structures of type point_t you’d need space in the amount of 10*sizeof(point_t).
Also recall that in order to use malloc() you need to
#include <stdlib.h>

Activity 26.5.

Write a function createPolygon() to which you can pass an integer and that reserves space in memory for an array of points of that size and returns a pointer to that array. Just to be on the safe side, have this function also initialize all coordinates to (0.0,0.0).

admin.....open in new window

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