M
mechanicfem
I thought (as they're in c99) that flexible arrays were there for a
number of reasons - but I also thought they'd be great for stepping
into structures (say) that were aligned in memory after (say) a header
struct, e.g.:
struct element
{
int a;
int b;
};
typedef struct element elem;
struct header
{
int a;
elem element[];
};
typedef struct header head;
....
head * p;
....
(p -> element)[10] = ...;
However, the same 'trick' can be done if header contained this
elem * element;
};
So, I can't really see the benefit of using empty array notation here.
And anyway, how would you malloc some memory for element when it's an
array type - surely p -> element = malloc... is an error, as element is
an array, and so can't be assigned to? That leaves me thinking that to
get it to work like that, maybe you should malloc a lump of memory,
then cast the address to a header *, access element, and then index
into that. But again, you could do this using straight forward pointer
notation in the declaration of element?
So, then I thought, well, maybe the notation would allow me to
initialise a struct ...
struct foo
{
int a;
char b[];
};
struct foo s = {10, {'f', 'o', 'o'}};
But gcc doesn't like that (with -std=c99), and again, if b were
declared char * b, surely this is illegal ...
struct foo s = {10, {"foo"}};
So, Istarting from 'they must be great for a number of reasons' I'm now
totally confused as to what flexible arrays can do for you. Could
someone please help me?
x
Jo
number of reasons - but I also thought they'd be great for stepping
into structures (say) that were aligned in memory after (say) a header
struct, e.g.:
struct element
{
int a;
int b;
};
typedef struct element elem;
struct header
{
int a;
elem element[];
};
typedef struct header head;
....
head * p;
....
(p -> element)[10] = ...;
However, the same 'trick' can be done if header contained this
elem * element;
};
So, I can't really see the benefit of using empty array notation here.
And anyway, how would you malloc some memory for element when it's an
array type - surely p -> element = malloc... is an error, as element is
an array, and so can't be assigned to? That leaves me thinking that to
get it to work like that, maybe you should malloc a lump of memory,
then cast the address to a header *, access element, and then index
into that. But again, you could do this using straight forward pointer
notation in the declaration of element?
So, then I thought, well, maybe the notation would allow me to
initialise a struct ...
struct foo
{
int a;
char b[];
};
struct foo s = {10, {'f', 'o', 'o'}};
But gcc doesn't like that (with -std=c99), and again, if b were
declared char * b, surely this is illegal ...
struct foo s = {10, {"foo"}};
So, Istarting from 'they must be great for a number of reasons' I'm now
totally confused as to what flexible arrays can do for you. Could
someone please help me?
x
Jo