#include <iostream.h>
struct Country
{
char *name;
};
bool AllocateMem (Country *countries);
void DeAllocateMem (Country *countries);
void PrintCountries (Country *countries);
void main()
{
Country* countries = new Country[10];
if (!AllocateMem(countries)) return;
countries[0].name = "Norway";
countries[1].name = "Sweden";
countries[2].name = "Denmark";
countries[3].name = "Finland";
countries[4].name = "Russia";
countries[5].name = "Germany";
countries[6].name = "England";
countries[7].name = "Ireland";
countries[8].name = "Scotland";
countries[9].name = "Holland";
PrintCountries(countries);
DeAllocateMem(countries);
return;
}
bool AllocateMem(Country *countries)
{
for (int i=0; i < 10; i++)
{
countries[i].name = new char[15];
if (countries[i].name == 0)
{
return false;
}
}
return true;
}
void DeAllocateMem(Country *countries)
{
for (int i=0; i < 10; i++)
{
delete [] countries[i].name;
countries[i].name = 0;
}
delete[] countries;
}
void PrintCountries(Country *countries)
{
for (int i=9; i >= 0; i--)
{
cout << countries[i].name << endl;
}
}
|