首页 > 其他 > 详细

匿名结构体

时间:2020-04-06 21:07:05      阅读:61      评论:0      收藏:0      [点我收藏+]
#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;
}

 

匿名结构体

原文:https://www.cnblogs.com/axuanup/p/12644029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!