Deutsch English Français Italiano |
<vji0lg$3i4tl$3@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: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: C89 "bug" Date: Fri, 13 Dec 2024 16:07:28 -0300 Organization: A noiseless patient Spider Lines: 86 Message-ID: <vji0lg$3i4tl$3@dont-email.me> References: <vjh8hu$3den0$1@dont-email.me> <87jzc3v48r.fsf@nosuchdomain.example.com> <vjhu52$3i4tq$1@dont-email.me> <vjhvnv$3igdc$1@dont-email.me> <vji06u$3i4tl$2@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 20:07:29 +0100 (CET) Injection-Info: dont-email.me; posting-host="8b9a0b17735b443b8fd85bb0477dd1c9"; logging-data="3740597"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18D4GEPXM8uwrsQLfybXqbB0rQ6TKN1u0M=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:yH2HEd51D7Ib3ShGL3FXcptYe3k= Content-Language: en-GB In-Reply-To: <vji06u$3i4tl$2@dont-email.me> Bytes: 3522 Em 12/13/2024 3:59 PM, Thiago Adams escreveu: > Em 12/13/2024 3:51 PM, David Brown escreveu: >> On 13/12/2024 19:24, Thiago Adams wrote: >>> 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. >>> >> >> Can you use gcc extensions here, or are you looking for strict C89 >> compliance? >> >> (To me, the "bug" is using C89 in the first place, but you have your >> reasons for that.) >> >> Another option if you are generating code is to make your union : >> >> union U { >> struct { unsigned int lo; unsigned int hi; } raw; >> int i; >> double d; >> }; >> >> and always initialise it with the underlying representation for the >> values and types that you want. (I believe you are generating the >> code, so that should be practical even for floating point data.) >> >> However, that puts a dependency on the endianness and size of types. >> > > Interesting! > Dependency on sizes is not a problem because the generated code is > intended to be used in a pipeline and discarded after feeding a C compiler. > Your suggestion is basically to have a struct of "words" that have the > same size of the structs. Then convert 1.2 (double) to two words. > It makes sense! > > > https://godbolt.org/z/cKb4j35W8 #include <stdio.h> union U { unsigned int _[2]; int i; double d; }; union U u1 = {1, 0}; /*1.2*/ union U u2 = {858993459, 1072902963}; /*1.2*/ int main(){ printf("u1.i = %d\n", u1.i); printf("u2.d = %f\n", u2.d); }