🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #14997

Re: Struct/Union avec les memes champs

#14997
From: Pierre Habouzit
Date: Wed, 19 Oct 2005 07:42
68 lines
2901 bytes
> typedef struct  {
>   int i;
>   union {
>     char j;
>     char  k;
>   };
> } foo;

>    assert( f.j == f.k); // Est-ce l�gal ? Si oui, ref ?
c'est l�gal, mais :
 * vu ton union toujours vrai
 * s�mantiquement faux.

un union, tu ne dois te servir que d'une des alternatives en m�me temps.
union ne sert en fait qu'� �viter de prendre trop de place dans un struct
lorsque tu sais que certains champs sont utilis�s de mani�re exclusive.

Typiquement, tu pourrais d�finir un type 'nombre' ainsi :

enum number_type { NUM_INT, NUM_FLOAT }

struct number {
  enum number_type type;
  union {
    int    ival;
    double dval;
  }
}

ici un nombre est �vidament soit un entier soit un flottant, de mani�re
exclusive.

Malheureusement, union n'est pas fait pour faire du structure aliasing (et
c'est bien dommage) par exemple :

struct {
    union {
        int array[3];
        struct {
            int first;
            int second;
            int third;
        } s;
    };
}

n'a � priori aucune raison d'�tre align� pareil d'apr�s la norme, et donc ne
te permet pas d'acc�der aux cases du tableau array soit via l'index, soit
via le nom de la case (personnellement je trouve �a tr�s dommage, mais
bon ... c'est la vie).

De plus, tu ne peux m�me pas faire ca :
struct {
    union {
        uint32_t arr32[N];
        uint16_t arr16[N*2];
        uint8_t  arr16[N*4];
    };
}

d�j� parce que en fait les uint*_t ne font pas forc�ment exactement * bits
en fait, mais surtout parce que tu vas alors rencontrer des probl�mes
d'endianess et donc �crire du code non portable si tu fais de tels alias.

-- 
�O�  Pierre Habouzit
��O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Technical Details
Message-ID: <4355f8d5$0$623$626a14ce@news.free.fr>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!infeed-2.proxad.net!news9-e.free.fr!not-for-mail
References: <slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain>

Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,eb7f26af12505976
X-Google-Attributes: gidf9822,public
X-Google-Language: FRENCH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!infeed-2.proxad.net!news9-e.free.fr!not-for-mail
Message-ID: <4355f8d5$0$623$626a14ce@news.free.fr>
From: Pierre Habouzit <madcoder@debian.org>
Subject: Re: Struct/Union avec les memes champs
Newsgroups: fr.comp.lang.c
Date: Wed, 19 Oct 2005 09:42:17 +0200
References: <slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain>
User-Agent: KNode/0.9.2
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8Bit
X-Face: $(^e[V4D-[`f2EmMGz@fgWK!e.B~2g.{08lKPU(nc1J~z\4B>*JEVq:E]7G-\6$Ycr4<;Z!|VY6Grt]+RsS$IMV)f>2)M="tY:ZPcU;&%it2D81X^kNya0=L]"vZmLP+UmKhgq+u*\.dJ8G!N&=EvlD
Lines: 68
Organization: Guest of ProXad - France
NNTP-Posting-Date: 19 Oct 2005 09:42:13 MEST
NNTP-Posting-Host: 82.225.205.10
X-Trace: 1129707733 news9-e.free.fr 623 82.225.205.10:58089
X-Complaints-To: abuse@proxad.net