C language community learning (one study)

C language community learning

Union is a special data type in C language, which allows different data types to be stored in the same memory space, but only one data type can be used at a time. The way to define a union is similar to defining a structure, except that the keyword is "union" instead of "struct".
The definition format of a union is as follows:

union union_name {
    type1 member1;
    type2 member2;
    ...
    typen membern;
};

Among them, union_name is the name of the union, and member1, member2, ..., member are the members of the union.
The size of the union is the size of the largest member among all members, because all members share the same memory space, so the values ​​of different members in the union may affect each other.
The method of using a union is similar to that of a structure, but it should be noted that the assignment to a member of the union will affect other members in the union, because they share the same memory space.
Unions have a wide range of application scenarios, and can be used to save memory space, and can also be used for operations such as type conversion.

Access member symbols.

To access members of a union, use the dot operator (.) or the arrow operator (->) to access union members. The dot operator is used to access members of a union variable, and the arrow operator is used to access members of a pointer to a union.
Here is sample code:

#include <stdio.h>

union myUnion {
    int num;
    char letter;
};

int main() {
    union myUnion u;
    u.num = 65;
    printf("Num value: %d\\n", u.num);
    printf("Letter value: %c\\n", u.letter);
    u.letter = 'B';
    printf("Num value: %d\\n", u.num);
    printf("Letter value: %c\\n", u.letter);
    return 0;
}

The above code creates a union myUnion, which contains an integer member num and a character member letter. In the main function, we first assign the num member a value of 65, and then use the dot operator to output the values ​​of the num and letter members. Next, we assign the value of the letter member to the character 'B', which will affect the value of the num member because they share the same memory space. Finally, we use the dot operator again to output the values ​​of the num and letter members.

Members of a union variable can be accessed using the dot operator (.), for example:

union myUnion {
    int num;
    char letter;
};

int main() {
    union myUnion u;
    u.num = 65;
    printf("Num value: %d\\n", u.num);
    printf("Letter value: %c\\n", u.letter); // 使用点运算符访问letter成员
    return 0;
}

In the above code, we define a union myUnion, which contains an integer member num and a character member letter. In the main function, we first assign the num member a value of 65, and then use the dot operator to output the values ​​of the num and letter members, such as u.numand u.letter.

The following are practical applications of unions in software programming:

#include <stdio.h>

union myUnion {
    int num;
    char letter;
};

int main() {
    union myUnion u;
    u.num = 65;
    printf("Num value: %d\\\\n", u.num);
    printf("Letter value: %c\\\\n", u.letter);
    u.letter = 'B';
    printf("Num value: %d\\\\n", u.num);
    printf("Letter value: %c\\\\n", u.letter);
    return 0;
}

The above code creates a union myUnion, which contains an integer member num and a character member letter. In the main function, we first assign the num member a value of 65, and then use the dot operator to output the values ​​of the num and letter members. Next, we assign the value of the letter member to the character 'B', which will affect the value of the num member because they share the same memory space. Finally, we use the dot operator again to output the values ​​of the num and letter members.

Unions are widely used in embedded programming. For example, you can use unions to manipulate bit-fields in hardware registers. Hardware registers typically contain multiple bit fields, each representing a different state or control. Using unions, each bit field can be easily accessed and set.

Here is an example code that uses a union to access the bit fields of a hardware register:

#include <stdio.h>

typedef union {
    unsigned char reg;
    struct {
        unsigned char bit0 : 1;
        unsigned char bit1 : 1;
        unsigned char bit2 : 1;
        unsigned char bit3 : 1;
        unsigned char bit4 : 1;
        unsigned char bit5 : 1;
        unsigned char bit6 : 1;
        unsigned char bit7 : 1;
    } bits;
} Register;

int main() {
    Register r;
    r.reg = 0x0A;
    printf("Bit 0: %d\\n", r.bits.bit0);
    printf("Bit 1: %d\\n", r.bits.bit1);
    printf("Bit 2: %d\\n", r.bits.bit2);
    printf("Bit 3: %d\\n", r.bits.bit3);
    printf("Bit 4: %d\\n", r.bits.bit4);
    printf("Bit 5: %d\\n", r.bits.bit5);
    printf("Bit 6: %d\\n", r.bits.bit6);
    printf("Bit 7: %d\\n", r.bits.bit7);
    return 0;
}

In the above code, we define a union Register, which contains an unsigned character member reg and a bit field structure bits. The bit field structure contains 8 bit fields, and each bit field represents a bit in reg. Using a bitfield structure, we can easily access and set each bit.

In the main function, we first assign the reg member to 0x0A, and then use the dot operator and the bit field name to output the value of each bit.

Note that when using a union to handle hardware registers, you need to ensure that the size of the union is the same as the size of the hardware register, and that the order and size of the bit fields are the same as those of the hardware register. Otherwise, undefined behavior or erroneous results may occur.

Guess you like

Origin blog.csdn.net/weixin_51624736/article/details/129493341