Article View: fr.comp.lang.c
Article #15511Re: champ de bits...
From: Charlie Gordon
Date: Sat, 24 Sep 2005 03:41
Date: Sat, 24 Sep 2005 03:41
78 lines
3055 bytes
3055 bytes
"Pierre Habouzit" <madcoder@debian.org> wrote in message
news:4334462d$0$24227$626a14ce@news.free.fr...
> Bouloun wrote:
>
> > Bonjour
> > J'ai un projet qui s'organise co �a:
> >
> > Main.C
> > Appl.c
> > Appl.h
> >
> > Dans Main .C j'utilise un champ de bit du genre : (d�laration faite
> > juste apr�s les variables globales)
> > struct
> > {
> > unsigned t1:1
> > unsigned t2:1
> > }cfg;
> >
> > Le probl�me c'est que le fichier Appl.c doit lui aussi pouvoir avaoir
> > acc�s � ce champ de bit.
> >
> > Comment faire pour pouvoir aussi utiliser ce champ de bit dans mon fichier
> > Appl.C ?
>
> d�j� c'est bizare de mettre des majuscules dans les noms de fichiers, mais
> c'est sans doute un d�tail.
D'autant que l'extension .C signifie C++ pour certains compilateurs unix.
> Ensuite, pour partager des d�finitions de type il faut les mettre dans des
> fichier .h (headers) que tu #include dans tes .h
>
> Enfin, ton code ne me parait pas correct, il faudrait plutot �crire :
>
> struct cfg {
> unsigned int t1:1;
> unsigned int t2:1;
> };
>
> ou bien :
>
> typedef struct {
> unsigned int t1:1;
> unsigned int t2:1;
> } cfg;
>
En fait la d�finition de l'OP ne pr�cise pas de tag pour la structure cfg. Il
me semble difficile de partager une telle chose sans lui donner un vrai type. A
part se reposer sur des mecanismes obsol�tes dont je tairai les d�tails ignobles
(common implicites), ou des d�clarations et definitions identiques mais
incompatibles, je ne crois pas que ce soit possible. En revanche lui donner un
tag suffit a r�soudre ce dilemne :
appl.h:
extern struct cfg_t {
unsigned t1:1;
unsigned t2:1;
} cfg;
----
main.c:
#include "appl.h"
struct cfg_t cfg;
----
appl.c:
#include "appl.h"
void set_t1(unsigned v) {
cfg.t1 = v;
}
Enfin unsigned est un synonyme de unsigned int, et il est en effet imp�ratif de
d�finir comme non sign�s les champs de 1 bit ;-)
--
Chqrlie.
Technical Details
Message-ID:
<dh2gbt$6f6$1@reader1.imaginet.fr>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!fr.colt.net!writer!not-for-mailReferences:
<433442ce$0$12992$7a628cd7@news.club-internet.fr> <4334462d$0$24227$626a14ce@news.free.fr>Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,f57b8b7d4d821213 X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!fr.colt.net!writer!not-for-mail From: "Charlie Gordon" <news@chqrlie.org> Newsgroups: fr.comp.lang.c Subject: Re: champ de bits... Date: Sat, 24 Sep 2005 05:41:09 +0200 Organization: ImagiNET / Colt Internet Lines: 80 Message-ID: <dh2gbt$6f6$1@reader1.imaginet.fr> References: <433442ce$0$12992$7a628cd7@news.club-internet.fr> <4334462d$0$24227$626a14ce@news.free.fr> NNTP-Posting-Host: gw.netgem.com X-Trace: reader1.imaginet.fr 1127531709 6630 195.68.2.34 (24 Sep 2005 03:15:09 GMT) X-Complaints-To: abuse@imaginet.fr NNTP-Posting-Date: 24 Sep 2005 03:15:09 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1506 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506