The C FAQ answers exactly this question. The quick answer is that this structure will include a double array inside the structure, not a pointer to an array outside the structure. As a quick example, you can use your structure, as in this example:
struct s mystruct = malloc(sizeof(struct s) + 5 * sizeof(double)); sn = 12; sd[0] = 4.0; sd[1] = 5.0; sd[2] = 6.0; sd[3] = 7.0; sd[4] = 8.0;
And so on - the size of the array that interests you is included in the distribution, and then you can use it just like any array. Typically, this type contains size as part of the structure, since using the + tag to skip an array of type s will necessarily be complicated by this situation.
In your added question: βHow is this construction more or less powerful than saving the pointer as the second element?β, It is not more powerful as such, but you do not need to hold the pointer, so you would save at least as much space - as well when you copy the structure, you also copied the array, not the pointer to the array - sometimes there would be a subtle difference, but a different time is very important. "You-can-do-it-in-multiple-way" is probably a good explanation, but there are times when you specifically want one design or another.
Carl Norum Jun 15 '10 at 17:36 2010-06-15 17:36
source share