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.
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:
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 answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 8 years ago.
i am trying to understand what is the difference when using structs and typedefs to access some components
what is the difference between using the dot operator when dealing with structs
using the example below
so far i have tried this code
typedef struct _game{
int something;
char something_else;
} g;
if i use
g.something or g->something
what is the difference?
i have used both of them and they both return results but i still dont understand the difference
can somebody please explain this?
I'm assuming this is C. When asking language questions tag the language. There are many languages that look the same and can give you subtly different answers. C++ is a different language than C, btw.
In this statement,
typedef struct _game { int something; } g;
g is a type, not a variable. As such, g.something makes no sense. typedef means "type define". Instead, you would have
g my_g_instance;
g *my_g_ptr = &my_g_instance;
my_g_instance.something = 2;
my_g_ptr->something = 5;
The difference between . and -> is whether the variable to the left of the operator is a pointer or not.
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.
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.