Array of structures. A pointer to a structure




array of structures with ++, массив структур c

Exactly the same, how we can create arrays of any built-in data types (int, char…), you can create arrays of structures. Consider the example of this structure WonderfulWoman familiar to us from first and second parts of the article on C++ structures.

It is necessary to make the data of 7 models. Data show on the screen in the form of a table, to make it easier to compare them. Schematically, so:

array of structures with ++, массив структур c  , pointer to the structure c ++

To implement this – dial code:

An array of structures WonderfulWoman declared in srtring 29 and in that same srtring all object elements in this array are initialized with zeroes. As you can see – Besides, built-in type instead, we wrote the descriptor structure, nothing new in this announcement is not for us. Given the name array, specify the number of elements (objects), initialized with zeroes – everything. Thus, we have created an array of structures.

How to refer to an element of a particular object structure? Average the dot is not suitable:

array of structures with ++, массив структур c  , pointer to the structure c ++

Of course, because it is not clear what exactly the object of 7 it is necessary to make the data. Since each object is now an array of cell structures, to refer to its elements must first specify the object code in brackets. Only after that point and the name of the element:

array of structures with ++, массив структур c  , pointer to the structure c ++

In strings 31- 49 is for loop. With it, you make the data in all of the objects in an array of structures. Note on a string 47. Without this operator cin.get(); you will encounter error, on which the compiler notifies you. It is, that after making the data element Woman[i].volume.hips and pressing Enter, in the input stream remains a symbol of the end of srtring. When the loop will continue to run – This symbol is automatically written in the Woman[i].name the next object, and we will immediately be prompted to enter the age of. That is the name we will not be able to make. To avoid this, it is necessary to read from the stream using this symbol cin.get(); .

Result – fill the structure with data :

array of structures with ++, массив структур c  , pointer to the structure c ++

After entering all the data models, the screen cleared (responsible for what system(“cls”);) and we shall see table:

array of structures with ++, массив структур c  , pointer to the structure c ++


In this article, let's talk more about pointers to structures. If, for example, it is necessary to write a function, which will be recorded in the data structure elements, You must pass this structure to a pointer. Otherwise, the changes will not be saved. Consider the example of the structure of the Size of the previous listing. It is necessary to write a function, wherein the user makes a data structure elements.

Declaring a pointer to a structure, do not forget, it is necessary to allocate memory for it – string 18. The cursor is declared. Let's see now the, how to access the elements of the structure, through a pointer. Suppose it is necessary to add value to the element waist. Use . – on the project access operation will not succeed:

array of structures with ++, массив структур c  , pointer to the structure c ++Error highlighting because, that should keep the pointer address, and he tries to access the element type int. Let's try to dereference a pointer to a structure:

array of structures with ++, массив структур c  , pointer to the structure c ++

Now everything is fine, but it is not convenient entry. In c++ There are special operation pointer access (arrow – It is consisting of dashes and greater sign) -> . It seems to be shown, on any element of the structure refers to the pointer:

array of structures with ++, массив структур c  , pointer to the structure c ++

So just remember – if you are working with a pointer to a structure, the point is not necessary to apply for access to the elements of the structure, and arrow. Rewrite example, which is above – so it will be easier to understand and remember.

Result:

array of structures with ++, массив структур c  , pointer to the structure c ++

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Date
page
An array of structures C ++. C ++ pointer to the structure
Rating
51star1star1star1star1star

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>