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.
using namespace std;
Sponsor* AddStruct(Sponsor* Obj, const int amount);
void setData(Sponsor* Obj, const int amount);
void showData(const Sponsor* Obj, const int amount);
Sponsor* OurSponsors = 0;
int sponsorAmount = 0;
int YesOrNot = 0; // продолжить или остановить ввод данных
OurSponsors = AddStruct(OurSponsors, sponsorAmount);
cout << "Продолжить ввод данных (1 - да, 0 - нет): ";
cin >> YesOrNot;
} while (YesOrNot != 0);
Sponsor* AddStruct(Sponsor* Obj, const int amount)
if (amount == 0)
Obj = new Sponsor[amount + 1]; // выделение памяти для первой структуры
Sponsor* tempObj = new Sponsor[amount + 1];
for (int i = 0; i < amount; i++)
tempObj[i] = Obj[i]; // копируем во временный объект
delete  Obj;
Obj = tempObj;
void setData(Sponsor* Obj, const int amount)
cout << "Фамилия: ";
cout << "Имя: ";
cout << "Сумма пожертвования: ";
cin >> Obj[amount].sum;
cout << endl;
void showData(const Sponsor* Obj, const int amount)
cout << "№ " << "Фамилия\t" << "Имя\t" << "Сумма\t" << endl;
cout << "========================================" << endl;
for (int i = 0; i < amount; i++)
cout << i + 1 << " " << Obj[i].surname << '\t' << Obj[i].name << '\t' << Obj[i].sum << endl;
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:
|Newsletter of programming: