Sujet : Re: C89 "bug"
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 13. Dec 2024, 19:50:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjhvm4$3i4tl$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
Em 12/13/2024 3:39 PM, bart escreveu:
On 13/12/2024 15:41, Thiago Adams wrote:
Em 12/13/2024 12:01 PM, Kaz Kylheku escreveu:
On 2024-12-13, Thiago Adams <thiago.adams@gmail.com> wrote:
unfortunately, this solution does not work if we have two objects.
>
union U {
double d;
int i;
};
>
union U u1 = { .d=2.2 };
union U u2 = { .i=1 };
>
Idea: have several declarations of the union in different translation
units.
>
/* translation unit for u1 */
union U {
double d;
int i;
};
extern union U u1 = { 2.2 };
>
/* translation unit for u2 */
union U {
int i;
double d;
};
extern union U u2 = { 1 };
>
>
another solution could be call a function that initializes before main.
>
extern union U u1;
>
void before_main()
{
u1.d = 1.2;
}
>
>
I assume the initialisation is at file-scope.
Then your suggestion could work, if you can arrange for it to be called.
But I think that will be a problem: this can occur in some arbitrary module compiled at a different time from the one containing the program's main(): either earlier, or later.
Yes..