Coder404 Coder404 - 1 year ago 100
C Question

How to pass a float array and a float as parameters to a function in C

I'm trying to send 2 parameters (a float and an array of floats) to a function in C.

I'm getting this error:

test.c:10:1: error: conflicting types for ‘result’
result(float *array, float number){
^
test.c:10:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
test.c:7:5: note: previous implicit declaration of ‘result’ was here
result(array, number);


My code is:

#include <stdio.h>
#include <math.h>

main()
{
float array[3] = {1.5, 2.6, 3.7};
float number = 2.3;
result(array, number);
}

result(float *array, float number)
{
printf("number: %f\n", number);
printf("array 1: %f\n", array[1]);
}


I'm new to C and know that in other languages this would work, so any help on what to do here would be much appreciated!

Answer Source

You can't access functions declared after main without a prototype. Rewrite your code like this:

#include <stdio.h>
#include <math.h>

int result(float *, float);

int main()
{
    /* ... */
}

int result(float *array, float number)
{
    /* ... */
}