The basics of programming in c++ for beginners

The dynamic array of structures C ++

The dynamic array of structures we analyze the example. We need to solve the following task: the user enters data about the sponsors of the project. Namely – surname, name and donation amount. After each data input program asked: continue typing or not.

Every time, when the user selects “continue” – It is necessary to allocate a piece of memory under one structure. Thus a dynamic array of structures will grow, until the user suspends entry. When you have finished entering, display a table with information about the sponsors on the screen.

Determination of the structure is in srtrings 5 – 10. It announced three elements name, surname, sum. Below the declared function prototypes, necessary to solve the task. The first function Sponsor* AddStruct(Sponsor* Obj, const int amount); will allocate memory for the elements of an array of structures. Second void setData(Sponsor* Obj, const int amount); is responsible for entering data into a structure. The third void showData(const Sponsor* Obj, const int amount); – It displays all the data in a table. Definitions of these functions will be discussed below.

To create a dynamic array of structures, must, as to create the usual dynamic array, declare a pointer. Only instead of a built-in type specify the descriptor structure – string 20. This pointer is nothing on points. It would be possible to allocate memory for an array of structures at once. For example:

динамический массив структур

But we will organize a more flexible allocation of memory for the dynamic array of structures – it will be allocated as needed. There is one sponsor – selects the memory under one structure. There are 3 sponsors – memory is allocated first under one structure, then under the second and further under the third. Everything will depend on – decides whether the user continue typing.

In strings 21 – 22, declared variables sponsorAmount – count the number of sponsors and YesOrNot – user selection (continue or interrupt input).

Strings 24 – 34: here is the loop do while. It runs until, until the user needs to enter data. In string 26 call function, which allocates memory for the structure OurSponsors = AddStruct(OurSponsors, sponsorAmount); Descend to its definition in the srtrings 42 – 61. Here it can be seen, that this function will return a pointer to a structure Sponsor.

It takes two parameters – a pointer to the structure and number of structures. When it is called for the first time – it will send an announcement in main pointer OurSponsors and variable sponsorAmount, which is zero. In the function block is executed if – selects the memory for a structure (strings 44 – 47). Then the function will return an address (pointer) this memory section and it will be recorded in OurSponsors – string 26.

In string 27 call function, which will allow to make a data structure. Its definition is in srtrings 63 – 73. After entering data, variable sponsorAmount is incremented. User offer make a choice – continue to enter or exit the. If we continue to – again, the function is called AddStruct().

It must be remembered, that pointer OurSponsors already refers to the memory location with recorded data. So do not just get reisolated memory. First, you need to take care about saving data. Look at the block else strings 48 – 59. In string 50 create a temporary pointer. Under it allocate memory for amount + 1 structures (i.e.. more than one structure, what took function). Next, copy the data from the received object.

Last object array structures tempObj left blank. When data is copied, frees memory Obj – string 57 and write in the new address pointer. He will point to memory, in which there are stored data and the additional allocated memory area for filling new data.

Once again, the function is called setData(), which will allow to make the data in a new dedicated section of storage – in the last element of the array of structures.

When a user decides to no longer enter data – He presses the zero. After this, work function showData() and the screen will display a table with the data from all the structures of dynamic array. At the end of the program do not forget to free memory, which takes a dynamic array of structures.

Result – data input:

c ++ dynamic array of structures


c ++ dynamic array of structures

The dynamic array of structures C ++
4 (80%) 12 vote[s]

6 thoughts on “The dynamic array of structures C ++

  1. Why not remove addstruct tempObj if at the end of all the values ​​have already been transferred to the Obj?

      1. A, excuse me, we are in a allocated for tempObj memory now stores an array!

      2. All, that you select in the program through the new operator, will not be deleted automatically, All these objects are allocated in a separate program “dog” memory, and you obliged to to remove them manually, delete operator, after, how they will eliminate the need.

      3. In this particular example tempObj – this is at the beginning of the pointer placing the array, you assign it to the same index Obj (who returned from the function AddStruct), tempObj pointer and the value itself is destroyed at the point of completion function AddStruct.

Leave a Reply

Your email address will not be published. Required fields are marked *