c语言程序设计_C语言程序错误
#include <stdio.h>
struct stu
{
char num[10];
char name[20];
char sex[5];
double score;
};
int main()
{
struct stu n[5]={
{"001","zhang san","boy",87},
{"002","li si","girl",34},
{"003","wan ma zi","boy",89},
{"004","zhao si","girl",49},
{"005","sun wu","boy",83}
};
struct stu *p,*p2;
p=&n,p=&n;
for(p=n[1];p<n[4];p++)
{
if(p2->score>p->score) p2->score=p->score;
}
printf("成绩最好的学生的信息为:\n");
printf("%s\t%s\t%s\t%f",p2->num,p2->name,p2->sex,p2->score);
return 0;
}
最佳答案
指针变量应赋值类型是地址而非立即数,
同时应注意数组名就是数组头也即数组空间的第一个元素的地址,
而且连续给同一个变量赋给同样的值是没有意义的,
【p=&n,p=&n;for(p=n[1];p<n[4];p++)...】应改为
【p=n,p2=n; for(p=&n[1];p<&n[4];p++)...】;
……
#include <stdio.h>
struct stu
{
char num[10];
char name[20];
char sex[5];
double score;
};
int main()
{
struct stu n[5]={
{"001","zhang san","boy",87},
{"002","li si","girl",34},
{"003","wan ma zi","boy",89},
{"004","zhao si","girl",49},
{"005","sun wu","boy",83}
};
struct stu *p,*p2;
p=n,p2=n;
for(p=&n[0];p<&n[4];p++)
{
if(p2->score < p->score) p2->score=p->score;
}
printf("成绩最好的学生的信息为:\n");
printf("%s\t%s\t%s\t%f\n",p2->num,p2->name,p2->sex,p2->score);
return 0;
}
其他回答
其它网友回答:
程序有多处错,作如下改动,即可正确运行:
其它网友回答:
#include <stdio.h>
其它网友回答:
struct stu
其它网友回答:
{
其它网友回答:
char num[10];
其它网友回答:
char name[20];
其它网友回答:
char sex[5];
其它网友回答:
double score;
其它网友回答:
};
其它网友回答:
int main()
其它网友回答:
{
其它网友回答:
struct stu n[5]={
其它网友回答:
{"001","zhang san","boy",87},
其它网友回答:
{"002","li si","girl",34},
其它网友回答:
{"003","wan ma zi","boy",89},
其它网友回答:
{"004","zhao si","girl",49},
其它网友回答:
{"005","sun wu","boy",83}
其它网友回答:
};
其它网友回答:
struct stu *p,*p2;
其它网友回答:
p=n,p2=n;
其它网友回答:
for(p=n;p<n+4;p++)
其它网友回答:
{
其它网友回答:
if(p2->score<p->score) p2=p;
其它网友回答:
}
其它网友回答:
printf("成绩最好的学生的信息为:\n");
其它网友回答:
printf("%s\t%s\t%s\t%f",p2->num,p2->name,p2->sex,p2->score);
其它网友回答:
return 0;
其它网友回答:
}
其它网友回答: