What does the colon do in this struct definition? [duplicate] - c

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.

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

What is the meaning of _Space in C? [duplicate]

This question already has answers here:
Why should we typedef a struct so often in C?
(15 answers)
Closed 20 days ago.
I have seen this expression in the code of other developer and I cannot get the meaning of it, the code line is:
typedef struct _Space Space;
So, for the syntax I reckon that _Space is a kind of variable or something similar but I do not know what kind of variable it is (integer, string, Boolean, etc).
Anyone have any idea??
struct _Space refers to a definition of a struct _Space elsewhere. The typedef ... Space means you can refer to a struct _Space as simply Space, saving you some typing.
For example, consider the difference in brevity and clarity between
struct _Space mySpace; // Oh god! memories
vs
Space mySpace;

Using typedef keyword in c [duplicate]

This question already has answers here:
How to create a typedef for function pointers
(3 answers)
Closed 5 years ago.
I was going through one of the lecture slide taught in the class which included a typedef keyword in it. As opposed to normal use of typedef keyword where we usually put a alias for particular data, there was no alias put up for that example. Following is the example:See the typedef in the slide
Is the use of typedef correct, or am I interpreting differently?
It is a function pointer. And it is perfectly valid syntax.
Ptr is declared as a pointer to a function where the function takes two int arguments and return an int. Here you can use Ptr as the alias to the typedefed type.

The dot operator in a struct [duplicate]

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.

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.

Resources