Re: C89 "bug"

Liste des GroupesRevenir à cl c  
Sujet : Re: C89 "bug"
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.c
Date : 13. Dec 2024, 20:07:28
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vji0lg$3i4tl$3@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
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);
}

Date Sujet#  Auteur
13 Dec 24 * C89 "bug"17Thiago Adams
13 Dec 24 +* Re: C89 "bug"6Michael S
13 Dec 24 i`* Re: C89 "bug"5Thiago Adams
13 Dec 24 i `* Re: C89 "bug"4Kaz Kylheku
13 Dec 24 i  `* Re: C89 "bug"3Thiago Adams
13 Dec 24 i   `* Re: C89 "bug"2bart
13 Dec 24 i    `- Re: C89 "bug"1Thiago Adams
13 Dec 24 +* Re: C89 "bug"8Keith Thompson
13 Dec 24 i`* Re: C89 "bug"7Thiago Adams
13 Dec 24 i +* Re: C89 "bug"5David Brown
13 Dec 24 i i+* Re: C89 "bug"2Thiago Adams
13 Dec 24 i ii`- Re: C89 "bug"1Thiago Adams
14 Dec 24 i i`* Re: C89 "bug"2bart
15 Dec 24 i i `- Re: C89 "bug"1David Brown
13 Dec 24 i `- Re: C89 "bug"1Keith Thompson
14 Dec 24 `* Re: C89 "bug"2Waldek Hebisch
14 Dec 24  `- Re: C89 "bug"1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal