#include <stdio.h>
#define N 20
#define M 3
//定义一个学生的数据类型
//属性1:姓名 char name[N]
//属性2:年龄 int age
//属性3:成绩 float scores[M]
//还可以用结构体来定义
/* struct student
* {
* char name[N];
* int age;
* float scores[M];
* }*/
typedef struct student
{
char name[N];
int age;
float scores[M];
}Stu;
int main(void)
{
Stu s1={"zhangsan",23,{12,3,2}};
int *pi=NULL;
Stu *ps=NULL;
ps=&s1;
int j=0;
printf("请输入姓名,年龄,三门成绩\n");
scanf("%s",ps->name);
scanf("%d",&ps->age);
for(j=0;j<M;j++)
{
scanf("%f",&ps->scores[j]);
}
printf("%s",s1.name);
printf("%d",s1.age);
for(j=0;j<M;j++)
{
printf("%3.0f",s1.scores[j]);
}
printf("\n");
printf("%s",(*ps).name);
printf("%d",(*ps).age);
for(j=0;j<M;j++)
{
printf("%3.0f",(*ps).scores[j]);
}
printf("\n");
printf("%s",ps->name);
printf("%d",ps->age);
for(j=0;j<M;j++)
{
printf("%3.0f",ps->scores[j]);
}
printf("\n");
return 0;
}
用子函数和结构体的结合实现下面程序
最新推荐文章于 2022-08-25 17:30:00 发布