Sujet : Re: C89 "bug"
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 13. Dec 2024, 19:24:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjhu52$3i4tq$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
Em 12/13/2024 3:15 PM, Keith Thompson escreveu:
Thiago Adams <thiago.adams@gmail.com> writes:
Does anyone knows how can I convert this code (external declaration) to C89?
>
union U {
int i;
double d;
};
>
union U u = {.d=1.2};
>
The problem is that in C89 only the first member of the union is
initialized.
The obvious solution is:
union U u;
u.d = 1.2;
But that works only if u has automatic storage duration.
You could also define a function that takes a double argument and
returns a union U result.
Like this?
union U {
int i;
double d;
};
union U f(){ union U u; u.d = 1.2; return u;}
union U u = f();
The problem is that f() is not a constant expression for external declarations.