结构体的应用

来源:百度知道 编辑:UC知道 时间:2024/05/28 20:03:09
#include<stdio.h>
#define MOON 3
struct student
{
int num;
char name[20];
int score[2];
}stu[MOON];
void main()
{
int i,air,j;
struct student * p;
p=stu;
for(i=0;i<MOON;i++)
{
printf("The number:");
scanf("%d",&p->num);
printf("\nThe name:");
scanf("%s",&p->name);
for(j=0;j<3;j++)
{
printf("The %d socre:",j+1);
scanf("The score:%d",&p->score[j]);

}
}
for(i=0;i<MOON;i++)
printf("%d\n%s\n%d\n%d\n%d\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
}
这个程序简直错误百出,自己都汗啊,希望高手帮帮忙哦。
输入成绩的那个循环似乎发挥作用太大了-_-!
而且还有很多其他问题...头都痛了...希望把程序弄正常了,最好有些讲解,十分感谢。
高原狼人用的是vc++吗?我运行了你的发现是错的啊
我希望用到指针啊,谢谢

你的程序我大致看明白了!

其核心问题在于你动用循环结构时没有使指针发生迁移!

程序给人总体印象就是乱!建议最好按要求重新写一个!

大致错误点:
1。结构体引用错误
2。数组下标错误
3。格式输入错误
4。指针使用不当

修改后的程序如下:

#include<stdio.h>
#define MOON 3

struct student
{
int num;
char name[20];
int score[3]; /* 原文 int score[2]; */
}; /* 原文 stu[MOON]; */

void main()
{
int i,j; /* 原文 int i,air,j; */
struct student *p,stu[MOON]; /* 原文 struct student *p; */
p=stu;
for(i=0;i<MOON;i++,p++) /* 原文 for(i=0;i<MOON;i++) */
{
printf("The number:");
scanf("%d",&p->num);
printf("\nThe name:");
scanf("%s",p->name); /* 原文 scanf("%s",&p->name); */
for(j=0;j<3;j++)
{
printf("The %d socre:",j+1);
scanf("%d",&p->score[j]); /* 原文 scanf("The score:%d",&p->s