当前位置:网站首页>[C language] anonymous/unnamed struct & Union

[C language] anonymous/unnamed struct & Union

2022-06-11 11:27:00 sidemap

Learning today muduo Code , It is found that member variables are declared in the following way in the class :

 private:
  union
  {
    struct sockaddr_in addr_;
    struct sockaddr_in6 addr6_;
  };

In the implementation of the class , It is directly used as a member variable addr_ and addr6_.

After investigation , Found out anonymous/unnamed struct&&union, Quote an online description :
Anonymous Union and Structure in C. In C11 standard of Canonymous Unions and structures were added. Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Definition is just like that of a normal union just without a name or tag.
translate :
C Anonymous associations and structures in . stay C11 In the standard , Add an anonymous union or structure . Anonymous consortiums or structures have no name, that is, anonymous consortiums or structures . Definition is like a normal Union , Just without name or tag.

In the past, it was declared that struct/union For a type , Then create variables with this type .

typedef union
{
    int a;
    float b;
} union_type;

union_type var;

above anonymous The way , It can be used directly in class objects obj.addr_ perhaps obj.addr6_.

Write a test program to explain how to use , It looks more intuitive .

#include <stdlib.h>
#include <stdio.h>

struct A1
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;
};

struct A2
{
    union   // 8
    {
        int i;
        long long l;
    };
};

struct A3
{
    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A12  // 4+8 => 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };
};

struct A13  // 4+ 8+8 => 8+ 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A23 // 8+ 8+8
{
    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A  // 4 + 8 + 8+8 => 8 + 8 + 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct B
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };

    struct __NamedStruct
    {
        int type_st_i;
        long long type_st_ll;
    };
};


int main()
{
    struct A a;
    a.__type.i = 4;
    printf("a.__type.i=%d\n", a.__type.i);

    printf("assign anonymous union.\n");
    a.i = 5;
    printf("a.__type.i=%d\n", a.__type.i);
    printf("a.i=%d\n", a.i);

    printf("assign anonymous structure.\n");
    a.st_i = 7;
    a.st_ll = 15;
    printf("a.__type.i=%d\n", a.__type.i);
    printf("a.i=%d\n", a.i);
    printf("a.st_i=%d\n", a.st_i);
    printf("a.st_ll=%lld\n", a.st_ll);

    printf("sizezof(struct A)=%lu\n", sizeof(struct A));        // 32
    printf("sizezof(struct B)=%lu\n", sizeof(struct B));        // 32

    printf("sizezof(struct A1)=%lu\n", sizeof(struct A1));      // 4
    printf("sizezof(struct A2)=%lu\n", sizeof(struct A2));      // 8
    printf("sizezof(struct A3)=%lu\n", sizeof(struct A3));      // 16
    printf("sizezof(struct A12)=%lu\n", sizeof(struct A12));    // 16
    printf("sizezof(struct A13)=%lu\n", sizeof(struct A13));    // 24
    printf("sizezof(struct A23)=%lu\n", sizeof(struct A23));    // 24

    return 0;
}

Through the above assignment and its occupied space , You can find that the corresponding object has been created .

But if you declare a type internally , No object of this type was created , Does not occupy the entire type of space ( Reference resources struct B And size A Of sizeof).

原网站

版权声明
本文为[sidemap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111058458521.html