C语言结构体作为C语言学习中的一个重要板块对后续的学习有着重要的作用,一下将介绍一个由C语言结构体实现的基本功能:记录并输出一个同学的基本信息。
#include<stdio.h>
typedef struct Birthday {
int year;
int month;
int day;
}Birthday;
typedef struct Student {
int id;
const char* name;//在这里要注意在使用一些编译环境(如vs)时要自己定义"const",以便于后续的强制类型转换。
int age;
int score;
Birthday birthday;
} Student;
void printStudent(Student *pStu)
{
printf("学号:%d,姓名:%s,成绩:%d,生日:%d-%d=%d\n",
pStu->id,pStu->name,pStu->score,//注意当将变量的地址传入时要使用"->",而非"."。
pStu->birthday.year,pStu->birthday.month,pStu->birthday.day);
}
int main()
{
Student stu1 = { 001,"阿兔",78,100,{2005,6,18}};//此处vs不显示".id"等,也不必手动补充。
Student stu2 = { 002,"毛熊",40,99,{2006,12,12}};
Student* pStu = &stu1;
printStudent(pStu);//调用函数。
pStu = &stu2;//更新地址。
printStudent(pStu);
return 0;
}