Arrays of Structures
The inflatable
structure contains an array (the name
array). It’s also possible to create arrays whose elements are structures. The technique is exactly the same as for creating arrays of the fundamental types. For example, to create an array of 100 inflatable
structures, you could do the following:
inflatable gifts[100]; // array of 100 inflatable structures
This makes gifts
an array of inflatable
s. Hence each element of the array, such as gifts[0]
or gifts[99]
, is an inflatable
object and can be used with the membership operator:
cin >> gifts[0].volume; // use volume member of first structcout << gifts[99].price << endl; // display price member of last struct
Keep in mind that gifts
itself is an array, not a structure, ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.