初学碰到C2120对于所有类型”void“非法
作者:互联网
对班级里的学生进行排序:按总分从高到低进行排序,如果总分一样,依次按语文、数学、英语分数从高到低进行排序;如果各科成绩都一样,则按名字的字典顺序从小到大排序。
#include<stdio.h>
#include<string.h>
#define N 4
typedef struct studentscore
{
char name[26];
int chinese;
int math;
int english;
int sum;
}stu;
stu class[N] = { {"liuyifei",66,66,66},{"zhaolinger",88,88,88},{"lixiaoyao",99,99,99},
{"zhangsan",33,33,33} };
void comp(stu p1, stu p2)
{
p1.sum = p1.chinese + p1.math + p1.sum;
p2.sum = p2.chinese + p2.math + p2.sum;
int comp = p1.sum - p2.sum;
comp = comp == 0 ? p1.chinese - p2.chinese : comp;
comp = comp == 0 ? p1.math - p2.math : comp;
comp = comp == 0 ? p1.english - p2.english : comp;
comp = comp == 0 ? strcmp(p1.name, p2.name) : comp;
return comp;
}
stu tmp;
int main()
{
for (int i = 1; i <N; i++)
{
for (int j = 0; j <N - i; j++)
{
if (comp(class[j], class[j + 1]) > 0)
{
tmp = class[j];
class[j] = class[j + 1];
class[j + 1]= tmp;
}
}
}
printf("%s %s %s %s", class[0].name,class[1].name,class[2].name,class[3].name);
return 0;
}
定义函数是忽略了函数中返回值类型,将函数定义为void,但是函数末尾return的返回值为int类型,与void相冲突;将void改为int即可解决问题。
标签:p2,C2120,int,comp,void,p1,初学,sum,class 来源: https://blog.csdn.net/SEexplode/article/details/122870007