Function of ":" in a structure

G

Guest

What is the function of ":" and followed number (2 or 1) in the followin
structure?

typedef struct {
unsigned int A : 2;
unsigned int B : 2;
unsigned int C : 2;
unsigned int D : 2;
unsigned int E : 1;
} ASampleStruct;
 
H

Howard

What is the function of ":" and followed number (2 or 1) in the followin
structure?

typedef struct {
unsigned int A : 2;
unsigned int B : 2;
unsigned int C : 2;
unsigned int D : 2;
unsigned int E : 1;
} ASampleStruct;

Those are "bit fields". They specify how many bits of the struct are
actually allocated for each of the members. They're useful sometimes when
you need a specific number of bits for your data. Read up about how to use
them in your favorite book.

By the way, you're using C-style notation for your struct definition, which
is kind of overkill. C+ structs are defined more simply, like this:

struct ASampleStruct
{
// add members here
};

-Howard
 

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top