Allocating a double pointer as an array
I'm trying to allocate a double pointer within a structure as an array. This is the function so far:
Code:
struct set
{
int filled;
int size;
char **data;
};
struct set *createSet(int maxElts)
{
struct set *sp;
if ((sp=malloc(sizeof(struct set)))==NULL)
{
printf("Failed to allocate memory!\n");
abort();
}
sp->filled=0;
sp->size=maxElts;
if ((sp->data=malloc(sizeof(char *)*maxElts))==NULL)
{
printf("Failed to allocate memory!\n");
abort();
}
if (sp->data[0]==NULL) //I wanted this to be allocated in such a way that I could use it as an array at this point. However, this is simply coming up as NULL.
printf("it isnt working\n");
return sp;
}
Anyone know how to do this? I know it is possible, since my instructor gave me the line that mallocs the double pointer with the intent to use it in this fashion.