Re: struct compatibility



Le 31-01-2006, Christian Kandeler <christian.kandeler@xxxxxx> a écrit :
> Hi,
>
> I'd like to know whether the following code is standard-compliant:
>
>
> struct generic {
> int x;
> int a[1];
> };
>
>
> void f(struct generic *s)
> {
> int i;
>
> for (i = 0; i < 10; i++)
> s->a[i] = i;
> }
>
>
> int main(void)
> {
> struct special {
> int x;
> int a[10];
> } s;
>
> f((struct generic *) &s);
>
> return 0;
> }

Interesting question...
If s was allocated on the heap, in C99, I think the code
would be standart because of compatibility with flexible
array member.

The standart says that, 6.7.2.1/16-18, if there exist
struct s {
int x;
int a[]; // No size !
};

Then, generic and s are 'compatible' when allocated
with malloc, and also are special and s.
In your example, there is no s, but I do not think
it will create any difference for compilers.


Marc Boyer
.



Relevant Pages