Deutsch English Français Italiano |
<vjhv0u$3i840$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: C89 "bug" Date: Fri, 13 Dec 2024 18:39:26 +0000 Organization: A noiseless patient Spider Lines: 55 Message-ID: <vjhv0u$3i840$1@dont-email.me> References: <vjh8hu$3den0$1@dont-email.me> <20241213145639.00003d71@yahoo.com> <vjhcle$3den0$2@dont-email.me> <20241213065614.739@kylheku.com> <vjhkig$3g4cu$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 13 Dec 2024 19:39:26 +0100 (CET) Injection-Info: dont-email.me; posting-host="87a54047bf3b6737c54eee95008b1c7c"; logging-data="3743872"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HERQGmpEg7fkmevlLun6M" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:hMNmj7PSizAD8azbUo6PSZj+XAw= In-Reply-To: <vjhkig$3g4cu$1@dont-email.me> Content-Language: en-GB Bytes: 2539 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. How will the compiler know which function(s) needs to be called from main(), when it translated the module containing main? I can think of an approach, but it's not simple, and may be over-kill if this is something that is infrequently encountered.