What does operator ":" inside a struct mean in C? [duplicate] - c

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.

Related

struct definition with ':' after name of the variable [duplicate]

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

Is it possible to pass a struct to a function without creating a variable? [duplicate]

This question already has an answer here:
Is it possible to pass a structure variable as a function argument without previously defining it?
(1 answer)
Closed 3 years ago.
In C++ this code is valid
struct foo{
int x;
};
int bar(foo f);
bar({1});
However I get an error when I try to do something similar in C. Is there way a to pass a struct to a function without actually creating a variable for it?
You need a compound literal for this:
bar((struct foo){1});

Typedef uint8 x[4] [duplicate]

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

C struct: what does this mean? [duplicate]

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.

C special syntax (adding ':' ) while defining a struct [duplicate]

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.

Resources