Article View: fr.comp.lang.c
Article #14999Re: Struct/Union avec les memes champs
From: Jean-Marc Bourguet
Date: Wed, 19 Oct 2005 10:06
Date: Wed, 19 Oct 2005 10:06
50 lines
2254 bytes
2254 bytes
Marc Boyer <Marc.Boyer@enseeiht.yahoo.fr.invalid> writes:
> Bonjour,
>
> deux questions distinctes mais assez proches sur les structs/union
> ayant les m�mes champs. Bien sur, ces codes compilent et fonctionnent
> que ma machine. Mais est-ce l�gal ?
>
> A) Struct qui commencent pareil
> Soient
> typedef struct { int i; } Si;
> typedef struct { int i; char c;} Sic;
> int main(){
> Sic sic;
> Si* psi= (Si*) &sic;
> psi->i= 1; // Est-ce l�gal ? Si oui, un ref ?
> }
J'ai le souvenir qu'il y a une regle pour que ce soit conforme dans le
cas ou les deux structs sont dans une union (en faisant une recherche
avec X11 hack tu devrais pouvoir arriver a la retrouver). Je ne sais
pas si c'est redige de telle maniere que ce le soit dans le cas que tu
donnes.
> B) Union avec les m�me champs
> #include <assert.h>
> typedef struct {
> int i;
> union {
> char j;
> char k;
> };
> } foo;
> int main(){
> foo f;
> f.j= 'a';
> assert( f.j == f.k); // Est-ce l�gal ? Si oui, ref ?
> return 0;
> }
Si j'ai bonne memoire, a part le cas auquel j'ai fait allusion
ci-dessus, une union pourrait avoir un champs cache ecrit quand on
assigne une valeur a l'union et verifie quand on fait une lecture.
A+
--
Jean-Marc
FAQ de fclc: http://www.isty-info.uvsq.fr/~rumeau/fclc
Site de usenet-fr: http://www.usenet-fr.news.eu.org
Technical Details
Message-ID:
<pxbveztddnr.fsf@news.bourguet.org>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!infeed-2.proxad.net!news6-e.free.fr!not-for-mailReferences:
<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!news1.google.com!proxad.net!infeed-2.proxad.net!news6-e.free.fr!not-for-mail Newsgroups: fr.comp.lang.c Subject: Re: Struct/Union avec les memes champs References: <slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain> From: Jean-Marc Bourguet <jm@bourguet.org> Message-ID: <pxbveztddnr.fsf@news.bourguet.org> Date: 19 Oct 2005 12:06:16 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Lines: 50 Organization: Guest of ProXad - France NNTP-Posting-Date: 19 Oct 2005 12:06:17 MEST NNTP-Posting-Host: 213.131.238.28 X-Trace: 1129716377 news6-e.free.fr 21621 213.131.238.28:49555 X-Complaints-To: abuse@proxad.net