Article View: fr.comp.lang.c
Article #15001Re: Struct/Union avec les memes champs
From: Marc Boyer
Date: Wed, 19 Oct 2005 11:53
Date: Wed, 19 Oct 2005 11:53
67 lines
2987 bytes
2987 bytes
Le 19-10-2005, Harpo <trashcan@hotmail.com> a �crit�:
> Marc Boyer wrote:
>
>>
>> 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'esp�re, Il n'y a rien de plus anodin. Si ce n'�tait pas l�gal les
> // prisons seraient pleines de programmeurs C.
Oui, mais j'aurais aim� retrouver une ref. Et je suis assez
mauvais en recherche dans la norme.
>> }
>>
>> 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 ?
>
> Que l'on me corrige s�v�rement si je dis une connerie. (Salut Emdel !)
> L'assert est l�gal, c'est syntaxiquement correct.
> Comme j et k sont du m�me type, on devrait m�me passer dessus sans
> encombres � l'�x�cution.
Sauf que f.k n'a peut-�tre pas �t� initialis�, donc, UB.
> Je pense m�me que l'on peut dire :
> assert( &(f.j) == &(f.k) ) quelque soit leur type du fait qu'ils sont au
> d�but de l'union.
C'est mon intuition en effet, et si on a &X == &Y, X et Y de m�me
type, alors X == Y.
> Le fait de ne pas nommer l'union doit �tre C99, si l'union avait �t�
> nomm�e u, on aurait pu dire :
> assert( &(f.u) == &(f.u.j) ) // yama
> si cela se passe dans les unions comme dans les structures.
Je sais plus de quand datent les unions anonymes. Je les ai
d�couvert r�cemment en ce qui me concerne.
>> return 0;
>
> No problem.
Ouf ;-)
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:
<slrndlcd38.a9d.Marc.Boyer@localhost.localdomain>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!134.158.69.22.MISMATCH!in2p3.fr!univ-lyon1.fr!jussieu.fr!crihan.fr!univ-lille1.fr!news.cict.fr!not-for-mailReferences:
<slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain> <435613e5$0$1716$8fcfb975@news.wanadoo.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!news2.google.com!proxad.net!134.158.69.22.MISMATCH!in2p3.fr!univ-lyon1.fr!jussieu.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 11:53:51 +0000 (UTC) Organization: CICT Lines: 67 Message-ID: <slrndlcd38.a9d.Marc.Boyer@localhost.localdomain> References: <slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain> <435613e5$0$1716$8fcfb975@news.wanadoo.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 1129722831 16282 147.127.19.155 (19 Oct 2005 11:53:51 GMT) X-Complaints-To: usenet@news.cict.fr NNTP-Posting-Date: Wed, 19 Oct 2005 11:53:51 +0000 (UTC) User-Agent: slrn/0.9.8.1 (Debian)