Reputation: 39
While compiling this code in the terminal, I am getting an error saying :
newfile1.c:17: error: conflicting types for ‘average’
newfile1.c:2: note: previous declaration of ‘average’ was here
I don't see what is wrong with the code. Could someone help me out?
enter code here
#include<stdio.h>
float average(float);
int main()
{
float marks[4],avg;
int i;
printf("Please enter your marks\n");
for(i=0;i<=3;i++)
{
scanf("%d",&marks[i]);
}
avg = average(marks[4]);
printf("The average marks value is %f",avg);
return 0;
}
float average(float a[4])
{
int i,sum;
float avg_m;
for(i=0;i<=3;i++)
{
sum=sum+a[i];
}
avg_m=sum/3;
return avg_m;
}
Upvotes: 0
Views: 99
Reputation: 2031
Replace
float average(float);
with
float average(float[]);
The function declaration and definition are not matching.
Then call the function like this:
avg = average(marks);
Upvotes: 9
Reputation: 481
In the prototype of average
, you have given float
as argument type so compiler is expecting a single float value as argument. If you want to pass an array of values, you have to declare your prototype like this:
float average(float input_marks[]);
You can't give length of an array argument in a prototype or definition. You have to pass array length as a separate argument. So your prototype should look something like
float average(float a[], int a_length);
Upvotes: 1
Reputation: 11232
Your function average
takes one float
as argument, hence the declaration should be floa avaerage(float)
. If you do float average(float a[4])
you are telling compiler that your function takes an array of 4 floats as argument.
Upvotes: 0
Reputation:
float average(float);
expects a float variable . You need to pass an array , so add
float average(float[]);
. Error happened since your function declaration and definition not matching.
in your main, you should call avg = average(marks);
to pass the array to function avg = average(marks[4]);
will pass a single variable.
Upvotes: 1
Reputation: 53326
Change line in your file
float average(float);
to
float average(float []);
You have declared the function to take one float
instead you want array of floats
.
Also, while calling it in main
, change to
avg = average(marks);
Upvotes: 2