I have a struct which contains a flexible member array:
typedef struct bar {
uint8_t bar_a;
uint16_t bar_b;
} bar_t;
typedef struct foo {
uint16_t foo_a;
uint_8_t n_bars;
bar_t *bars[];
} foo_t;
I'm trying to use foo
and bar
as data structures to copy data over to uint8_t buffer[50]
.
#define NUMBER_OF_BARS 1;
static uint8_t buffer[50];
static foo_t *
copy_to_buf(void *buf)
{
int i;
foo_t *foo = (foo_t *)buf;
bar_t bar;
/* Set foo_a */
foo->foo_a = 1;
foo->n_bars = NUMBER_OF_BARS;
/* Set bars */
for(i=0; i < foo->n_bars; i++) {
bar.bar_a = 0xFF;
bar.bar_b = 1234;
memcpy(&foo->bars[i], &bar, sizeof(bar_t));
}
return foo;
}
int main()
{
int i;
foo_t *foo = copy_to_buf(&buffer);
printf("foo ... [%p %p %p %p]\n",
&foo, &foo->foo_a, &foo->n_bars &foo->bars);
for(i=0; i < foo->n_bars; i++) {
printf("bar ... [%p %p %p]\n",
&foo->bars[i], &foo->bars[i]->bar_a, &foo->bars[i]->bar_b);
}
return 0;
}
Now I would have expected this to print the following (i.e. a contiguous block of memory):
foo ... [0x1f92 0x1f92 0x1f94 0x1f95]
bar ... [0x1f95 0x1f95 0x1f96]
Instead, I'm getting the following:
foo ... [0x1f92 0x1f92 0x1f94 0x1f95]
bar ... [0x1f95 0x0 0x1]
What am I doing wrong?