C语言结构体基础(1)

       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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值