What is the use of ':' in type definition?

Discussion in 'C Programming' started by fl, Aug 13, 2011.

  1. fl

    fl Guest

    Hi,

    I am reading a C source code generated from Matlab "coder generation
    tool", see below please.


    typedef struct {
    real_T Pv;
    real_T Rg;
    real_T cnt;
    uint_T is_c1_c4e : 4;
    uint_T is_active_c1_c4e : 1;
    } SFc1_c4eInstanceStruct;


    I do not understand the ':' in last two line definitions. I guess it
    is about 4 bytes, or 4X(16 bit word) alignment? Please tell me some
    because I do not use C a lot. Thanks.
    fl, Aug 13, 2011
    #1
    1. Advertising

  2. fl

    Nobody Guest

    On Sat, 13 Aug 2011 06:49:53 -0700, fl wrote:

    > uint_T is_c1_c4e : 4;
    > uint_T is_active_c1_c4e : 1;
    > } SFc1_c4eInstanceStruct;
    >
    >
    > I do not understand the ':' in last two line definitions. I guess it
    > is about 4 bytes, or 4X(16 bit word) alignment? Please tell me some
    > because I do not use C a lot. Thanks.


    It indicates a bitfield, i.e. 4 bits and 1 bit respectively.
    Nobody, Aug 13, 2011
    #2
    1. Advertising

  3. fl

    Eric Sosman Guest

    On 8/13/2011 9:49 AM, fl wrote:
    > Hi,
    >
    > I am reading a C source code generated from Matlab "coder generation
    > tool", see below please.
    >
    >
    > typedef struct {
    > real_T Pv;
    > real_T Rg;
    > real_T cnt;
    > uint_T is_c1_c4e : 4;
    > uint_T is_active_c1_c4e : 1;
    > } SFc1_c4eInstanceStruct;
    >
    >
    > I do not understand the ':' in last two line definitions. I guess it
    > is about 4 bytes, or 4X(16 bit word) alignment? Please tell me some
    > because I do not use C a lot. Thanks.


    Close -- but they're counting bits, not bytes. The is_c1_c4e
    element uses four bits, and is_active_c1_c4e uses one. It looks
    like the first can hold a value from 0 through 15, and the second
    is probably a true-or-false flag indicating something about the
    status of the first. Look up "bit field" (or "bit-field") in your
    C textbook or reference.

    As an aside, the output of a code generation tool is usually
    not "source" from a human perspective, even if it's "source" to a
    compiler. The true source is whatever you fed into the tool, and
    that's the source you should probably be reading unless you're
    trying to debug the tool itself.

    (Very many years ago, someone asked for help debugging a truly
    mystifying problem. I studied his source code, and the mysteries
    just kept mounting up: I simply could not reconcile the output with
    the code he showed me. Turned out he'd been putting his source
    through the first phase of the compiler, hand-editing the assembly
    "source" it generated, assembling and running the edited stuff, and
    then bringing the original source to me for help. It's a good thing
    there were no sharp implements close to hand when I figured out what
    he'd been up to ...)

    --
    Eric Sosman
    d
    Eric Sosman, Aug 13, 2011
    #3
  4. "fl" <> schrieb im Newsbeitrag
    news:...
    ....
    > typedef struct {
    > real_T Pv;
    > real_T Rg;
    > real_T cnt;
    > uint_T is_c1_c4e : 4;
    > uint_T is_active_c1_c4e : 1;
    > } SFc1_c4eInstanceStruct;

    ....
    "Nobody" and Eric already explained that is_c1_c4e and uint_T
    is_active_c1_c4e are bit fields. They are often used in small embedded
    systems to save storage or in memory-mapped I/O registers. These two fields
    of SFc1_c4eInstanceStruct occupy only a single byte. So if e.g.
    is_c1_c4e = 8 (binary 1000) and is_active_c1_c4e = 1,
    then the byte is 24 = 0x18 (binary 00011000) on Turbo C 2.0 .
    Heinrich Wolf, Aug 13, 2011
    #4
  5. fl

    Eric Sosman Guest

    [OT] Re: What is the use of ':' in type definition?

    On 8/13/2011 2:28 PM, Aaron W. Hsu wrote:
    > On Sat, 13 Aug 2011 10:57:36 -0400, Eric Sosman
    > <> wrote:
    >
    >> Very many years ago, someone asked for help debugging a truly
    >> mystifying problem. I studied his source code, and the mysteries
    >> just kept mounting up: I simply could not reconcile the output with
    >> the code he showed me. Turned out he'd been putting his source
    >> through the first phase of the compiler, hand-editing the assembly
    >> "source" it generated, assembling and running the edited stuff, and
    >> then bringing the original source to me for help. It's a good thing
    >> there were no sharp implements close to hand when I figured out what
    >> he'd been up to ...

    >
    > I would have probably invoked the “idiot†clause in my contract and
    > charged him out the wazoo for that sort of thing.


    Had he not been a tenured professor with some influence over
    my continued employment, "idiot" might have escaped the fence of
    my teeth, yes.

    --
    Eric Sosman
    d
    Eric Sosman, Aug 13, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stanimir Stamenkov
    Replies:
    2
    Views:
    716
    Stanimir Stamenkov
    Oct 25, 2005
  2. Jianli Shen
    Replies:
    1
    Views:
    557
    Victor Bazarov
    Mar 13, 2005
  3. Ark
    Replies:
    1
    Views:
    398
    Chris Torek
    Aug 7, 2004
  4. Jon Slaughter
    Replies:
    4
    Views:
    422
    Jon Slaughter
    Oct 26, 2005
  5. Pierre Yves
    Replies:
    2
    Views:
    468
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page