//尝试使用const关键词,并把结构体变量定义在函数内,避免影响封装
#include<stdio.h>
struct score
{
int chinese;
int english;
int math;
};
struct student
{
double num;
char name[20];
struct score subjects; //结构体嵌套
};
int main()
{
struct student *p,person[5];
p=person;
int i,j;
for(i=0;i<5;i++,p++)
{
scanf("%ld %s",&p->num,p->name);
scanf("%d %d %d",&p->subjects.chinese,&p->subjects.english,&p->subjects.math);
}
printf("------下面是输出结果------\n");
p=person;
void output(const struct student person[]);//尝试使用const关键词
output(p); //结构体变量做函数参数
return 0;
}
void output(const struct student *p)
{
int i,j;
for(i=0;i<5;i++,p++)
{
/*printf("%ld %s\n",p[i].num,p[i].name);*/
printf("%ld %s\n",p->num,p->name);
/*printf("%d %d %d\n",p[i].subjects.chinese,p[i].subjects.english,p[i].subjects.math);*/
printf("%d %d %d\n",p->subjects.chinese,p->subjects.english,p->subjects.math);
}
}
C语言【结构体练习改进】//尝试使用const修饰结构指针,并把结构体变量定义在函数内,避免影响封装
最新推荐文章于 2025-05-05 16:46:09 发布
本文通过一个具体的例子介绍了C语言中如何定义和使用结构体,包括结构体的嵌套以及如何在函数间传递结构体变量。此外,还展示了如何使用const关键字来保护数据不被修改。
1712

被折叠的 条评论
为什么被折叠?



