Reputation: 33727
When I have an array in a struct, the meaning is totally clear to me: When the struct is defined, memory for the whole array is reserved and when I copy the struct, all the array contents is copied.
typedef struct {
uint8_t type;
struct {
uint8_t length;
uint8_t data[5];
} m;
} s;
But when I use uint8_t data[]
, what does that mean? I guessed it might be the same as uint8_t *data
but it isn't. When I try to assign to it like this:
s the_s;
uint8_t something[] = {1, 2, 3};
the_s.m.data = something;
the compiler gives me
cannot assign array type objects
Upvotes: 1
Views: 2148
Reputation: 81347
An incomplete array type is a reference to the location where the first item of an array would have been allocated, but does not actually allocate space for that item. On some older C compilers, one could get a similar effect by declaring an array of size zero, although doing so has never been legal in any "official" version of the C standard. The primary use for such declarations is for structures which will be allocated using malloc, calloc, or other similar mechanism; the code which allocates space for the struct will allocate enough extra space to handle the desired number of array items.
Before incomplete array declarations became legal in C, a common workaround was to declare an array of size 1, and then subtract one from the number of elements to be appended to the struct. For example:
struct { int this,that,whatever; char name[1]; } MYSTRUCT; void test(char *new_name, int new_name_length) { MYSTRUCT *ms = malloc(sizeof(MYSTRUCT)+new_name_length-1); memcpy(ms->name, new_name, new_name_length); }
This approach had a couple of icky aspects, however:
If C compilers had simply not included code to reject zero-size arrays, and the standard had specified that the maximum subscript value for any array is (unsigned int)(size-1), the struct hack would have been much cleaner. Unfortunately, the standard wasn't written that way.
Upvotes: 3
Reputation: 400159
Your assumption that data[]
is the same as *data
is simply incorrect.
An unspecified array length can be used when dynamically allocating a structure that ends in an array, but it never means you get an assignable pointer.
Upvotes: 2
Reputation: 145919
An array of an incomplete type as the last member of a struct is a C99 feature called the flexible array member feature.
In this statement
the_s.m.data = something;
you are trying to assign an array but in C, arrays cannot be assigned.
Upvotes: 4