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
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):
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:
And the code for "myStruct" becomes:
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:
should be read as "a pointer to array(s) of unknown size of pointers to structures", rather than
which is "a pointer to pointer(s) to structures", or (e.g.):
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:And finally, the test code for either architecture:
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:
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 havingmyStruct *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 dosizeof
on it to find the size.