匿名结构体

#include<stdio.h>
#include<string.h>

//使用匿名结构体嵌套
struct person1
{
    const char* name;
    char gender[20];

    struct
    {
        int age;
    };
}p1;

//不使用匿名结构体嵌套

struct phone
{
    int  area_code;
    long phone_number;
};

struct person2
{
    const char* name;
    char gender[20];
    struct phone p;
}p2;


int main(void)
{
    //匿名结构体访问成员变量
    p1.name = "pppp";
    printf("%s
", p1.name);
    strcpy(p1.gender, "aaaa");
    printf("%s
",p1.gender);
    p1.age = 44;
    printf("%d
",p1.age);    //访问二层结构体

    //不使用匿名结构体访问成员变量
    p2.p.area_code = 100;
    printf("%d
", p2.p.area_code);    //访问二层结构体
    
    return 0;
}
原文地址:https://www.cnblogs.com/axuanup/p/12644029.html