🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #15404

Re: champ de bits...

#15404
From: Antoine Leca
Date: Mon, 26 Sep 2005 08:55
104 lines
4547 bytes
En <news:dh2gbt$6f6$1@reader1.imaginet.fr>, Charlie Gordon va escriure:
> Pierre Habouzit wrote in message
> news:4334462d$0$24227$626a14ce@news.free.fr...
>> Bouloun wrote:
>>>     Dans Main .C j'utilise un champ de bit du genre : (d�laration faite
>>> juste apr�s les variables globales)

En fait, cfg _est_ une variable globale suppl�mentaire.


>>> struct {
>>>     unsigned t1:1
>>>     unsigned t2:1
>>>     }cfg;
>>
>> 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.


Un truc typiquement C :

appl.h:
EXTERN struct cfg_t {
    unsigned t1:1;
    unsigned t2:1;
} cfg;

----------------------------

main.c:
#define EXTERN        /*d�finit les objets*/
#include "appl.h"

----------------------------

appl.c:
#define EXTERN extern /*d�clare seulement */
#include "appl.h"


Cela ne permet pas d'initialiser cfg autrement qu'avec des 0, mais cela
permet quand m�me d'utiliser un champ de bits sans donner d'�tiquette
explicite � la structure.
Ce n'est pas du meilleur style, mais si vous rencontrez cela, sachez que
cela marche.


> A part se reposer sur des mecanismes obsol�tes dont je tairai les
> d�tails ignobles (common implicites),

En effet, c'est aussi une autre solution, non totalement portable (en gros,
r�serv� � *nix).


> ou des d�clarations et definitions identiques mais incompatibles,
> je ne crois pas que ce soit possible.

Pourquoi incompatibles ?

    6.2.7 Compatible type and composite type

   [#1] Two types have compatible type if their types are the same.
     Additional rules for determining whether two types are
     compatible are described in 6.7.2 for type specifiers, in 6.7.3
     for type qualifiers, and in 6.7.5 for declarators.*) Moreover,
     two structure, union, or enumerated types declared in separate
     translation units are compatible if their tags and members
     satisfy the following requirements: If one is declared with a
     tag, the other shall be declared with the same tag. If both are
     complete types, then the following additional requirements
     apply: there shall be a one-to-one correspondence between their
     members such that each pair of corresponding members are
     declared with compatible types, and such that if one member of
     a corresponding pair is declared with a name, the other member
     is declared with the same name. For two structures,
     corresponding members shall be declared in the same order. For
     two structures or unions, corresponding bit-fields shall have
     the same widths. For two enumerations, corresponding members
     shall have the same values.

On dirait que les conditions sont soit remplies, soit inapplicables.

C'est bien s�r moins beau que le fait de mettre une �tiquette (qui est une
bonne habitude � prendre par ailleurs, et qui permet de faire des
initialisations explicites simplement).


Antoine
Technical Details
Message-ID: <dh8d1v$c2k$1@shakotay.alphanet.ch>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!freenix!news.alphanet.ch!alphanet.ch!not-for-mail
References: <433442ce$0$12992$7a628cd7@news.club-internet.fr> <4334462d$0$24227$626a14ce@news.free.fr> <dh2gbt$6f6$1@reader1.imaginet.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!news2.google.com!proxad.net!freenix!news.alphanet.ch!alphanet.ch!not-for-mail
From: "Antoine Leca" <root@localhost.invalid>
Newsgroups: fr.comp.lang.c
Subject: Re: champ de bits...
Date: Mon, 26 Sep 2005 10:55:26 +0200
Organization: Posted through ALPHANET (http://www.alphanet.ch/)
Lines: 105
Sender: antoinel@143.red-217-127-250.staticip.rima-tde.net
Message-ID: <dh8d1v$c2k$1@shakotay.alphanet.ch>
References: <433442ce$0$12992$7a628cd7@news.club-internet.fr> <4334462d$0$24227$626a14ce@news.free.fr> <dh2gbt$6f6$1@reader1.imaginet.fr>
NNTP-Posting-Host: 143.red-217-127-250.staticip.rima-tde.net
Mime-Version: 1.0
Content-Type: text/plain;
 charset="iso-8859-15"
Content-Transfer-Encoding: 8bit
X-Trace: shakotay.alphanet.ch 1127724927 12372 217.127.250.143 (26 Sep 2005 08:55:27 GMT)
X-Complaints-To: usenet@alphanet.ch
NNTP-Posting-Date: Mon, 26 Sep 2005 08:55:27 +0000 (UTC)
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