为什么所得结果永远是0????
本帖最后由 WFR 于 2020-4-7 22:36 编辑有4名学生输入名字和成绩,用自定义的函数输出成绩中最大的。
#include<stdio.h>
struct student //定义结构体;
{
char name;
int score;
} stu,*p;
main()
{
int max(int *p);
p = &stu;
int i;
printf("四位学生的姓名和成绩分别为:");
for (i = 0; i < 4; i++) //输入数据
scanf("%s,%d", &stu.name, &stu.score);
printf("四位学生中成绩最高为:%d",max(p));
}
max(int *p) //进行比较
{
int i, m ;
for (i = 0,m=4; i<4,m>0;m--, i++)
{
if (stu.score>stu.score)
m = i;
}
return stu.score;
} 数据还没输入你就先调用了max函数? BngThea 发表于 2020-4-7 21:03
数据还没输入你就先调用了max函数?
?????? WFR 发表于 2020-4-7 21:04
??????
应该怎么改????求教大佬 WFR 发表于 2020-4-7 21:05
应该怎么改????求教大佬
sorry,看错了,第一行是声明
你的max函数中根本没有传入p的必要,函数体中根本没用到
或者整个代码来看,p都没有存在的必要,因为你将stu定义为全局变量
试试改一下max函数:
int max() //进行比较
{
int i;
int score = stu.score;
for (i = 1;i<4;i++)
{
if (stu.score>score)
score = stu.score;
}
return score;
}
BngThea 发表于 2020-4-7 21:14
sorry,看错了,第一行是声明
你的max函数中根本没有传入p的必要,函数体中根本没用到
没用,输出得结果还是0 警告 2 warning C4047: “函数”:“student *”与“int”的间接级别不同 W:\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.c 14 1 ConsoleApplication2
警告 4 warning C4028: 形参 1 与声明不同 W:\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.c 17 1 ConsoleApplication2
警告 3 warning C4024: “max”: 形参和实参 1 的类型不同 W:\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.c 14 1 ConsoleApplication2
调用了个寂寞{:10_266:}{:10_266:}{:10_266:}{:10_266:}{:10_266:} 传参数都没给结构体里面的吧! scanf()
里面有逗号,键盘输入不写逗号的问题,没逗号就空格隔开
页:
[1]