Reputation: 1
I keep getting this error: [Error] conflicting types for 'average_grade' and i cant find my mistake.. i am new in C so i really need some help here.
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);
}
Upvotes: 0
Views: 2331
Reputation: 12263
As you do not pass more information, I suspect the error here:
struct card *first ... mo=average_grade(&first) ... float average_grade(struct card *arxh)
You pass struct card **
("pointer to pointer to struct ..") to a function requiring struct card *
("pointer to struct ..").
As you do not change arxh
, you might want mo=average_grade(first)
.
Notet that the prototypes are missing. I presume thy are given in advance of the posted code.
Note: You should always post a MCVE. That example is far from that. You also show not if/wnat you tried to find out yourself.
Hint:
Always enable warnings. At least -Wall
(for gcc) or similar for your compiler. More warnings can be helpful, check the available options of your compiler.
Upvotes: 1
Reputation: 121387
In C, if there's no visible prototype found at the time of calling a function, compiler implicitly declares prototypes (pre-C99 -- Since C99, the implicit int rule has been removed) with int
return type.
But when the actual definitions are found later, their type (float
s) conflict with the ones compiler declared for you. So, declare function prototypes at the beginning of the file (or put them in a header file) or move the functions above main()
.
Upvotes: 1