Article View: fr.comp.lang.c
Article #14998Re: Struct/Union avec les memes champs
From: Harpo
Date: Wed, 19 Oct 2005 09:37
Date: Wed, 19 Oct 2005 09:37
45 lines
2129 bytes
2129 bytes
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.
> }
>
> 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.
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.
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.
> return 0;
No problem.
Technical Details
Message-ID:
<435613e5$0$1716$8fcfb975@news.wanadoo.fr>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fdn.fr!news.wanadoo.fr!news.wanadoo.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!fdn.fr!news.wanadoo.fr!news.wanadoo.fr!not-for-mail Message-ID: <435613e5$0$1716$8fcfb975@news.wanadoo.fr> From: Harpo <trashcan@hotmail.com> Subject: Re: Struct/Union avec les memes champs Newsgroups: fr.comp.lang.c Date: Wed, 19 Oct 2005 11:37:41 +0200 References: <slrndlbo6u.9sr.Marc.Boyer@localhost.localdomain> Organization: ARG ; Association pour la Rehabilitation du Goto User-Agent: KNode/0.8.2 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit Lines: 45 NNTP-Posting-Date: 19 Oct 2005 11:37:42 CEST NNTP-Posting-Host: 86.195.226.91 X-Trace: 1129714662 news.wanadoo.fr 1716 86.195.226.91:32902 X-Complaints-To: abuse@wanadoo.fr