I understand more about pointers and stuff but I have no idea what I am doing wrong here.
if i Have
char *(*data)[]
That would just be interpreted as "a pointer to an array of char pointers", right?
Then I have a struct like this, typedef'd to be myStruct, redundant as it may be, but that's aside the point:
typedef struct myStruct myStruct;
struct myStruct{
int size;
char *name;
myStruct *(*array)[];
}
Having looked around the site for similar posts, I got something like this:
//let's say allocating 5 spaces for this case
myStruct *a = malloc(sizeof(myStruct)+ sizeof(struct myStruct *)*5);
I am sure that the number I allocated the struct with is the size of the array.
I can't quite get my head wrapped around this, how does it work if it's a struct?
The plan here is to have this struct, and it contains an array of 5 myStruct's. Do I have to allocate them separately as well? like this?
a->array[0] = malloc( .... )
I tried and it keeps giving me an error saying Invalid use of array with unspecified bounds.
What am I doing wrong or how Can i fix this?
Thank you
myStruct *(*array)[];
is not a flexible array member, since it's not an array type. It is a pointer which happens to be pointing to an incomplete array type.
The general pattern for flexible array member is:
struct myStruct {
int size;
char *name;
Type array[];
};
where in your case, Type would be defined by typedef MyStruct * Type;
. I'm assuming you want an array which will contain 5 pointers. (This is the same effect as having myStruct *array[];
in the struct, of course).
(If you do actually want your struct to contain a single pointer, which points to an array of 5 elements; then flexible array member is not the right technique to use).
Your malloc is correct for this definition I have just given. It allocates a contiguous bloc of memory and you can use array
as if it were actually an array of 5 objects, except you can't do sizeof
on it to find the size.
From your comment, it sounds like you want a pointer to an array of pointers to structures, rather than a pointer to an array of structures, since "pointer to an array of char *" also has two levels of indirection.
Here's the difference:
A pointer to a structure:
A pointer to an array of structures:
A pointer to an array of pointers to structures:
Assuming you want #3, you can do it like so (in "traditional" C):
typedef struct myStruct myStruct;
struct myStruct
{
int size;
char *name;
myStruct **array;
};
myStruct *allocate_node(char *name, int size)
{
myStruct *p_node;
if (size < 0)
size = 0;
p_node = calloc(1, sizeof(myStruct));
p_node->name = name;
p_node->size = size;
p_node->array = calloc(1, size * sizeof(myStruct *));
return p_node;
}
void expand_node_child_array(myStruct *p_node, int size_to_add)
{
if (size_to_add < 1 || p_node == NULL)
return;
if (p_node->array == NULL)
{
p_node->size = size_to_add;
p_node->array = calloc(1, size_to_add * sizeof(myStruct *));
}
else
{
p_node->array = realloc(p_node->array, (p_node->size + size_to_add) * sizeof(myStruct *));
memset(p_node->array + p_node->size * sizeof(myStruct *), 0, size_to_add * sizeof(myStruct *));
p_node->size += size_to_add;
}
}
myStruct *get_child_node(myStruct *p_node, int index)
{
if (index < 0 || index >= p_node->size)
return 0;
return p_node->array[index];
}
int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
if (index < 0 || index >= p_node->size)
return FALSE;
p_node->array[index] = p_child;
return TRUE;
}
void free_node(myStruct **pp_node)
{
// Free p_node and the array but DO NOT free the children
if (pp_node == NULL || *pp_node == NULL)
return;
if ((*pp_node)->array != NULL)
free((*pp_node)->array);
free((*pp_node));
*pp_node = NULL;
}
void free_node_and_children(myStruct **pp_node)
{
int iChild;
if (pp_node == NULL || *pp_node == NULL)
return;
for (iChild = 0; iChild < (*pp_node)->size; iChild++)
{
myStruct *p_child = get_child_node((*pp_node), iChild);
if (p_child != NULL)
free_node_and_children(&p_child);
set_child_node((*pp_node), iChild, NULL);
}
free_node(pp_node);
}
Update
A flexible array, under the C99 standard syntax, is a variable-length array that appears at the tail of a structure and whose actual length is set at run time. It looks like this in memory:
Assuming your compiler supports this syntax (not all do), you declare it like this:
struct myStruct
{
Type array_of_type[]; /* AT THE END OF THE STRUCT ONLY */
};
And the code for "myStruct" becomes:
typedef struct myStruct myStruct;
struct myStruct
{
int size;
char *name;
myStruct *array[];
};
myStruct *allocate_node(char *name, int size)
{
myStruct *p_node;
if (size < 0)
size = 0;
p_node = calloc(1, sizeof(myStruct) + size * sizeof(myStruct *));
p_node->name = name;
p_node->size = size;
return p_node;
}
myStruct *get_child_node(myStruct *p_node, int index)
{
if (index < 0 || index >= p_node->size)
return NULL;
return p_node->array[index];
}
int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
if (index < 0 || index >= p_node->size)
return FALSE;
p_node->array[index] = p_child;
return TRUE;
}
void free_node(myStruct **pp_node)
{
if (pp_node == NULL || *pp_node == NULL)
return;
free((*pp_node));
*pp_node = NULL;
}
void free_node_and_children(myStruct **pp_node)
{
int iChild;
if (pp_node == NULL || *pp_node == NULL)
return;
for (iChild = 0; iChild < (*pp_node)->size; iChild++)
{
myStruct *p_child = get_child_node((*pp_node), iChild);
if (p_child != NULL)
free_node_and_children(&p_child);
set_child_node((*pp_node), iChild, NULL);
}
free_node(pp_node);
}
If you compiler does not, see here for some workarounds.
With flexible arrays, expanding the array would require re-allocating the node itself and fixing all references to it, something not required in the "pointer to array of pointers" design.
The syntax you are using:
myStruct *(*array)[];
should be read as "a pointer to array(s) of unknown size of pointers to structures", rather than
myStruct **array;
which is "a pointer to pointer(s) to structures", or (e.g.):
myStruct *(*array)[4];
which is "a pointer to array(s) of length 4 of pointers.
Your syntax actually produces memory map #3, however accessing the individual elements is a bit more awkward because you have to explicitly get a pointer to the zeroth element of the "array of unknown size", which is (*p_node->array)
. Thus the functions from #3 are modified as follows:
void expand_node_child_array(myStruct *p_node, int size_to_add)
{
if (size_to_add < 1 || p_node == NULL)
return;
if (p_node->array == NULL)
{
p_node->size = size_to_add;
p_node->array = calloc(1, size_to_add * sizeof(myStruct *));
}
else
{
p_node->array = realloc(p_node->array, (p_node->size + size_to_add) * sizeof(myStruct *));
memset((*p_node->array) + p_node->size * sizeof(myStruct *), 0, size_to_add * sizeof(myStruct *));
p_node->size += size_to_add;
}
}
myStruct *get_child_node(myStruct *p_node, int index)
{
if (index < 0 || index >= p_node->size)
return NULL;
return (*p_node->array)[index];
}
int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
if (index < 0 || index >= p_node->size)
return FALSE;
(*p_node->array)[index] = p_child;
return TRUE;
}
And finally, the test code for either architecture:
void dump_nodes_recursive(myStruct *p_node, int level)
{
if (p_node == NULL)
{
printf("%*s", 4*level, " ");
printf("NULL\n");
}
else
{
int iChild;
printf("%*s", 4*level, " ");
printf("Node: Name=\"%s\", array size=%d\n", p_node->name, p_node->size);
for (iChild = 0; iChild < p_node->size; iChild++)
{
myStruct *p_child = get_child_node(p_node, iChild);
printf("%*s", 4*level, " ");
printf("Child [%d]:\n", iChild);
dump_nodes_recursive(p_child, level+1);
}
}
}
void dump_nodes(myStruct *p_node)
{
dump_nodes_recursive(p_node, 0);
}
void test_my_struct()
{
myStruct *p_top = allocate_node("top", 4);
myStruct *p_child0 = allocate_node("child0", 1);
myStruct *p_child1 = allocate_node("child1", 5);
myStruct *p_child2 = allocate_node("child2", 0);
myStruct *p_child3 = allocate_node("child3", 0);
myStruct *p_child00 = allocate_node("child00", 0);
set_child_node(p_top, 0, p_child0);
set_child_node(p_top, 1, p_child1);
set_child_node(p_top, 2, p_child2);
set_child_node(p_top, 3, p_child3);
set_child_node(p_child0, 0, p_child00);
dump_nodes(p_top);
free_node_and_children(&p_top);
}