I have the following struct:
typedef struct _chess {
int **array;
int size;
struct _chess *parent;
} chess;
and I have:
typedef struct _chess *Chess;
Now, I want to create an array of dynamic length to store pointers to the chess struct so I do the following:
Chess array [] = malloc(size * sizeof(Chess));
This gives me an error: invalid initializer.
And if I drop the [] and do this:
Chess array = malloc(size * sizeof(Chess));
it compiles without error but when I try to set an element of this array to NULL by doing:
array[i]=NULL;
I get an error: incompatible types when assigning to type ‘struct _chess’ from type ‘void *’
Any idea what am I doing wrong?
Thanks.
arrayis a slightly misleading name. For a dynamically allocated array of pointers,mallocwill return a pointer to a block of memory. You need to useChess*and notChess[]to hold the pointer to your array.and perhaps later: