Re: C parse error, why?



-----Original Message-----
From: Nengming Zhang <znm@china.com>
To: gtk-list@gnome.org <gtk-list@gnome.org>
Date: 26 August 2000 07:16
Subject: C parse error, why?


>
>Sorry to ask this simple question!
>
>I don't do  how ":" works In C program like the following example:
>
>int y:1;                          /*  parse error*/
>struct _Test
>{
>        int x :1;                /* no parse error report*/
>}


The syntax x:1 is used to declare a bitfield -- that is, a way of allocating
less than a single byte for a variable.  The compiler packs bitfield
variables together inside an integral number of bytes, which is why it
doesn't make sense to have a bitfield variable on its own, only as part of a
structure.  For example,

struct _Test {
    int x:4;
    int y:4;
};

The whole structure only occupies 1 byte -- 4 bits are allocated to each
variable.  If the structure only contained the x variable, it would still
occupy a byte -- the last 4 bits would be unused.

Hope this helps,

Glenn






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]