This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
What does the : do in a struct declaration after a member
(1 answer)
What does 'unsigned temp:3' in a struct or union mean? [duplicate]
(4 answers)
Closed 9 years ago.
typedef struct{
unsigned flanke:1;
unsigned lastState:1;
} flanke_t;
I do not understand the ":1". Please help me, thx.
These are bit fields: https://en.wikipedia.org/wiki/Bit_field. Here you just reserve 1 bit for 'flanke' and one for the 'lastState'. The type has to be unsigned int.
What you see here is bit field declaration usage. it is used to indicate the number of bits a given structure member will occupy in the structure so its main usage is to pack a structure so that it occupies less memory.
Related
This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 1 year ago.
I'm reading about structs in C and I came across some interesting declaration that I don't know what does is mean. The declaration goes as follows:
typedef struct name{
int x :1;
}name;
what does line 'int x :1;' mean ? is 1 the default value of x?
Would appreciate help! Thanks
It specifies the number of bits to use for that field. It is called "bit field".
Check this: https://en.cppreference.com/w/cpp/language/bit_field
This question already has answers here:
typedef fixed length array
(7 answers)
Closed 4 years ago.
I found this line of code and I didn't understand how can you typedef a uint8 to something that isn't a type in the first place like x[4]?
What is really meant here ? Is it an just an array of 4 uint8 type variables and that is just another way of saying it?
Code:
Typedef uint8 x[4]
The syntax of a typedef matches the syntax of a declaration. In this case, x is a type alias for uint8[4].
This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 6 years ago.
I'm working with a piece of code and I come across with something similar to this:
struct {
int a : 1;
int b : 1;
};
I don't know what is the function of : 1 here. For your interest, that struct is inside a union which in turn is inside a typedef struct.
Thanks,
This is used to assign value to a bit. You can read about bitfields here.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
What does the : do in a struct declaration after a member
I would like to ask why the : character was added in this struct:
typedef union A
{
struct
{
ubyte B:4;
}
} struct_a;
Thanks in advance;
The :4 is putting a 4-bit limit on the variable. See Section 6.9 of Kernighan & Ritchie.
It's called a bit field. In this case it is saying the B is 4 bits in size.
It declares a bit field with 4 bits.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does 'unsigned temp:3' means
I don't understand this struct definition. It seems illegal to me, but apparently it isn't:
typedef struct
{
unsigned i:1;
} my_struct;
I believe that marking the variable as unsigned without a type is the same as marking it as unsigned int. However, I'm completely baffled by the colon. What does it do? My guess is that it's some sort of initializer, but I haven't a clue what it means. Off the top of my head, the only place that I'd expect a colon in C is in the ternary operator, which this obviously isn't. So, this appears to be a construct with which I am not familiar. Can anyone explain what the purpose of the colon is in this struct definition? What exactly does the declaration unsigned i:1; mean?
It defines i to be of 1 bit width.
If i:x is given then it defines i to be x bits wide.