Paminu wrote:
If I have this struct:
#include <stdlib.h>
#include <stdio.h>
#define KIDS 4
typedef struct test {
int x;
int y;
} container;
--
I would now like to make an array containing 4 pointers to this struct. But
I would also like to allocate space for this array:
int main(void)
{
container *new[4];
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!
As it should. You have already declared new as an array[4] of pointers
to container.
{
size_t i;
for (i = 0; i < 4; i++)
if (!(new[i] = malloc(sizeof *new[i])))
{ /* handle error */ }
}
return 0;
}
Do I have to allocate space for each pointer at a time?
No, you need to allocate space for what each pointer points to.