C基础笔记(结构体的使用与嵌套)

结构体的使用

结构的使用

在定义结构的同时,可以生成几个变量

别名typedef

结构是可以嵌套的,结构中还可以使用结构

复制代码
#include <stdio.h>
#include<string>
typedef struct tagdate{
    int year;
    int month;
    int day;
}date;
typedef struct tagstudent{
  char name[20];
  int age;
  char sex[4];
  date birthday; //使用data 结构作为student的成员
}student;
int main(){
    student zs;
    strcpy_s(zs.name, "张三");
    strcpy_s(zs.sex, "");
    zs.age=18;
    zs.birthday.year=1990;
    zs.birthday.month=3;
    zs.birthday.day=4;
    student ss[3] = {
        {"陈俊泽",23,"",{1999,01,03}},
        {"陈  泽",23,"",{1998,01,03}},
        {"陈  俊",23,"",{1997,01,03}},
    };
    for (int i = 0; i < 3; i++) {
        printf("name=%s,age=%d,sex=%s,birthyear=%d,birthmonth=%d,birthday=%d\n", 
                ss[i].name, ss[i].age, ss[i].sex, ss[i].birthday.year, ss[i].birthday.month, ss[i].birthday.day);
    }
    return 0;

}
复制代码

结果:

name=陈俊泽,age=23,sex=男,birthyear=1999,birthmonth=1,birthday=3
name=陈    泽,age=23,sex=男,birthyear=1998,birthmonth=1,birthday=3
name=陈    俊,age=23,sex=男,birthyear=1997,birthmonth=1,birthday=3

posted @   罗悠然  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示