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

F

fl

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.
 
N

Nobody

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.
 
E

Eric Sosman

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 ...)
 
H

Heinrich Wolf

....
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 .
 
E

Eric Sosman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top