Article View: fr.comp.lang.c
Article #15003Re: Struct/Union avec les memes champs
From: Marc Boyer
Date: Wed, 19 Oct 2005 12:05
Date: Wed, 19 Oct 2005 12:05
53 lines
2482 bytes
2482 bytes
Le 19-10-2005, Pierre Habouzit <madcoder@debian.org> a �crit�:
>> 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.
Non, je suis pas d'accord: soit c'est un UB (acc�s � f.k
non initialis�), soit c'est vrai.
> 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.
Ca c'est l'usage typique. Mais est-ce qu'on peut le d�tourner ?
> 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
Oui, parce qu'il peut mettre du padding entre first et second,
mais pas entre array[0] et array[1].
Mais quand on fait:
union {
struct {
int premier;
} french;
struct {
int first;
} english;
}
On peut (cf 6.5.2.3/5 et /7 donn�s par Jean-marc).
Marc Boyer
--
� v�lo, prendre une rue � contre-sens est moins dangeureux
que prendre un boulevard dans le sens l�gal. � qui la faute ?
Technical Details
Message-ID:
<slrndlcdoq.a9d.Marc.Boyer@localhost.localdomain>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fdn.fr!freenix!nocworld.net!ens.insa-rennes.fr!news.univ-brest.fr!crihan.fr!univ-lille1.fr!news.cict.fr!not-for-mailReferences:
<slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain> <4355f8d5$0$623$626a14ce@news.free.fr>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!news1.google.com!proxad.net!fdn.fr!freenix!nocworld.net!ens.insa-rennes.fr!news.univ-brest.fr!crihan.fr!univ-lille1.fr!news.cict.fr!not-for-mail From: Marc Boyer <Marc.Boyer@enseeiht.yahoo.fr.invalid> Newsgroups: fr.comp.lang.c Subject: Re: Struct/Union avec les memes champs Date: Wed, 19 Oct 2005 12:05:20 +0000 (UTC) Organization: CICT Lines: 53 Message-ID: <slrndlcdoq.a9d.Marc.Boyer@localhost.localdomain> References: <slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain> <4355f8d5$0$623$626a14ce@news.free.fr> Reply-To: Marc.Boyer@enseeiht.yahoo.fr (del yahoo) NNTP-Posting-Host: ubu.enseeiht.fr Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: news.cict.fr 1129723520 16282 147.127.19.155 (19 Oct 2005 12:05:20 GMT) X-Complaints-To: usenet@news.cict.fr NNTP-Posting-Date: Wed, 19 Oct 2005 12:05:20 +0000 (UTC) User-Agent: slrn/0.9.8.1 (Debian)