What is the purpose of the union within the structure here?

typedef struct {
    union {
        u32 slock;
        struct __raw_tickets {
#ifdef __ARMEB__
            u16 next;
            u16 owner;
#else
            u16 owner;
            u16 next;
#endif
        } tickets;
    };                                                                                                                                  
} arch_spinlock_t;

Above is a snippet of code from the Linux kernel. What is the purpose of placing the entire structure within the structure. Why not just combine it?

This is a link to the source code.

+4
source share
2 answers

The Linux kernel is work in progress, not the see-how-it-should-be thing.

I believe that there is no true reason for this, other than the habit of packing everything in structon a higher level. If you thought that “everything is there struct”, you can accidentally lose by adding a new field to the actual unionone and not to structas you expected.

+1
source

, , , - , . , int ( , 1,5), ( , )?

- - .

, , , .

C- , ( ) , , structs , , .

:

typedef union {
    int units;
    float kgs;
} amount ;

typedef struct {
    char selling[15];
    float unitprice;
    int unittype;
    amount howmuch;
} product;
0

Source: https://habr.com/ru/post/1621426/


All Articles