匿名结构体

#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\n", p1.name);
    strcpy(p1.gender, "aaaa");
    printf("%s\n",p1.gender);
    p1.age = 44;
    printf("%d\n",p1.age);    //访问二层结构体

    //不使用匿名结构体访问成员变量
    p2.p.area_code = 100;
    printf("%d\n", p2.p.area_code);    //访问二层结构体
    
    return 0;
}

 

posted @ 2020-04-06 20:17  Axuanup  阅读(890)  评论(0编辑  收藏  举报