[英]Conflicting types for “function” error (C)
我不断收到此错误:[错误]'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语言中,如果在调用函数时找不到可见的原型,则编译器会使用int
返回类型隐式声明原型(C99之前的版本-自C99起,隐式int规则已被删除)。
但是,当稍后找到实际的定义时,它们的类型( float
)与为您声明的编译器冲突。 因此,在文件的开头声明函数原型(或将它们放在头文件中)或将函数移到main()
上方。
由于您没有传递更多信息,因此我怀疑这里的错误:
struct card *first ... mo=average_grade(&first) ... float average_grade(struct card *arxh)
您将struct card **
(“指向struct ..的指针”)传递给需要struct card *
(“指向struct ..的指针”)的函数。
由于不更改arxh
,因此可能需要mo=average_grade(first)
。
注意,缺少原型。 我想你是在发布代码之前给出的。
注意:您应该始终发布MCVE 。 这个例子远非如此。 您也不会显示是否/试图发现自己。
暗示:
始终启用警告。 对于您的编译器,至少-Wall
(对于gcc)或类似版本。 更多警告可能会有所帮助,请检查编译器的可用选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.