我不断收到此错误:[错误] 'average_grade' 类型冲突 我找不到我的错误..我是 C 的新手,所以我真的需要一些帮助。
struct card {
char on[20];
char ep[20];
float b;
int ap;
struct card *next;
};
struct card *first,*last,*t;
int ch;
int main()
{
float mo;
do {
printf("\n1.Initialize\n2.Add to end\n3.Show list\n4.Average Grade\n0.Exit\nChoice:");
scanf("%d",&ch);
switch(ch) {
case 1: init_list(&first,&last);
break;
case 2: t=create_node();
add_to_end(t,&first,&last);
break;
case 3: show_list(first);
break;
case 4: mo=average_grade(&first);
printf("%f",&mo);
break;
case 0: printf("Bye!\n");
break;
default:printf("Try again.\n");
break;
} /* switch */
} while (ch!=0);
system("pause");
return 0;
}
float average_grade(struct card *arxh)
{
struct card *i;
float sum=0;
int cnt=0;
for (i=arxh; i!=NULL; i=i->next)
{
sum= sum + i->b;
cnt++;
}
return sum/cnt;
}
void init_list(struct card **arxh, struct card **telos)
{
*arxh=NULL;
*telos=NULL;
}
struct card *create_node()
{
struct card *r;
r=(struct card *)malloc(sizeof(struct card));
printf("Give data:");
scanf("%s %s %f %d",r->on,r->ep,&r->b,&r->ap);
r->next=NULL;
return r;
}
void add_to_end(struct card *neos,struct card **arxh,struct card **telos)
{
if (*arxh==NULL)
{
*arxh=neos;
*telos=neos;
}
else
{
(*telos)->next=neos;
*telos=neos;
}
}
void show_list(struct card *arxh)
{
struct card *i;
for (i=first; i!=NULL; i=i->next)
printf("%s %s %.1f %d\n",i->on, i->ep, i->b, i->ap);
}
最佳答案
在 C 中,如果在调用函数时没有找到可见的原型(prototype),编译器会使用 int
隐式声明原型(prototype)(C99 之前——从 C99 开始,隐式 int 规则已被删除)返回类型。
但是当稍后找到实际定义时,它们的类型(float
)与为您声明的编译器冲突。因此,在文件开头声明函数原型(prototype)(或将它们放在头文件中)或将函数移至 main()
之上。
关于 "function"错误的冲突类型 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823556/