As a homework I have to write a program that operates around structures. As a first task i have to write a function that allocates memory for an array of N pointers that point to new structures(user decides about the value of N) and returns the adress of an array. The first problem that i have is understanding the form of malloc. I asked my professor what would be the equivalent by using "new" because it is more transparent for me but he answered that I should stick to malloc so i avoid making any mistakes. The following function looks like this:
struct Structure
{
int a;
char b;
float c;
};
Structure** allocating(int N)
{
struct Structure** tab = (struct Structure**) malloc(amount * sizeof(struct Structure*));
for (int i = 0; i < N; i++)
{
tab[i] = (struct Structure*) malloc(sizeof(struct Structure));
}
return tab;
}
I have tried understanding this form of allocating memory but so far i understand this as if i was allocating memory for a pointer pointing to the array of pointers(double **) which is not what was stated in a task. To sum up, i don't understand the way the allocating is written and how could it be written by using new.