Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9346 > unrolled thread

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

Started byfl <rxjwg98@gmail.com>
First post2011-08-13 06:49 -0700
Last post2011-08-13 17:40 +0200
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  What is the use of ':' in type definition? fl <rxjwg98@gmail.com> - 2011-08-13 06:49 -0700
    Re: What is the use of ':' in type definition? Nobody <nobody@nowhere.com> - 2011-08-13 15:10 +0100
    Re: What is the use of ':' in type definition? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-13 10:57 -0400
      Re: What is the use of ':' in type definition? "Aaron W. Hsu" <arcfide@sacrideo.us> - 2011-08-13 14:28 -0400
        [OT] Re: What is the use of ':' in type definition? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-13 17:39 -0400
    Re: What is the use of ':' in type definition? "Heinrich Wolf" <invalid@invalid.invalid> - 2011-08-13 17:40 +0200

#9346 — What is the use of ':' in type definition?

Fromfl <rxjwg98@gmail.com>
Date2011-08-13 06:49 -0700
SubjectWhat is the use of ':' in type definition?
Message-ID<a46e730f-78dc-4188-b837-71b3221a68fa@k15g2000yqd.googlegroups.com>
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.

[toc] | [next] | [standalone]


#9347

FromNobody <nobody@nowhere.com>
Date2011-08-13 15:10 +0100
Message-ID<pan.2011.08.13.14.10.04.898000@nowhere.com>
In reply to#9346
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.

[toc] | [prev] | [next] | [standalone]


#9348

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-08-13 10:57 -0400
Message-ID<j263j2$uq9$1@dont-email.me>
In reply to#9346
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#9352

From"Aaron W. Hsu" <arcfide@sacrideo.us>
Date2011-08-13 14:28 -0400
Message-ID<op.vz5z94jl0p3ku8@localhost>
In reply to#9348
On Sat, 13 Aug 2011 10:57:36 -0400, Eric Sosman  
<esosman@ieee-dot-org.invalid> 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.

	Aaron W. Hsu

-- 
Programming is just another word for the lost art of thinking.

[toc] | [prev] | [next] | [standalone]


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

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-08-13 17:39 -0400
Subject[OT] Re: What is the use of ':' in type definition?
Message-ID<j26quh$l5h$1@dont-email.me>
In reply to#9352
On 8/13/2011 2:28 PM, Aaron W. Hsu wrote:
> On Sat, 13 Aug 2011 10:57:36 -0400, Eric Sosman
> <esosman@ieee-dot-org.invalid> 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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#9349

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2011-08-13 17:40 +0200
Message-ID<j265u9$7le$1@news.m-online.net>
In reply to#9346
"fl" <rxjwg98@gmail.com> schrieb im Newsbeitrag 
news:a46e730f-78dc-4188-b837-71b3221a68fa@k15g2000yqd.googlegroups.com...
...
> 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 .

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web