It is a small story. Not particularly worth reading.
Let's delete unnecessary names from the structure with such a definition.
(N and bit1 are used, so do not delete them.)
typedef struct _Foo {
    int n;
    int unused;
    unsigned bit1:8;
    unsigned unusedBit:8;
} Foo;
Foo g_foo;
typedef)Very rarely, some people think that a structure cannot be defined without typedef, but it is not required.
In that case, specify the type name as struct. (Can be omitted in C ++.)
struct Foo {
    // ...
};
struct Foo g_foo;
You can use it by omitting the struct identifier and typedefing the unnamed struct.
typedef struct {
    // ...
} Foo;
Foo g_foo;
In the case of using a struct only once (for example, defining a global variable and then referencing a member), the struct does not need a name. You can define a structure with a variable.
struct {
    // ...
} g_foo;
From now on, we will use this code piece.
Although it is a processing system extension, you can delete the unnecessary member name ʻunused, leaving only the type.  In this case, the member is treated as non-existent. (It is also excluded from the number of bytes in sizeof`.)
_ Honestly, I don't know where this grammar is useful. _
struct {
    int n;
    int; // <--
    unsigned bit1:8;
    unsigned unusedBit:8;
} g_foo;
I don't use it for padding etc., but I think that members may be added for adjustment. You can remove the name ʻunusedBit` of an unwanted member of a bitfield. In this case, the number of bits is reserved and the area cannot be accessed by name.
struct {
    int n;
    int unused;
    unsigned bit1:8;
    unsigned :8;
} g_foo;
_ // I recently learned that this way of writing is possible, so I wrote this article as a memo. _
It will be like this. If you're new to it, it may be confusing, but it's (should) still be compliant (other than "erasing member names").
struct {
    int n;
    int;
    unsigned bit1:8;
    unsigned :8;
} g_foo;
        Recommended Posts