如何将float数组和float作为参数传递给C [复制]中的函数

时间:2022-12-08 19:48:13

This question already has an answer here:

这个问题在这里已有答案:

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

我正在尝试将2个参数(浮点数和浮点数组)发送到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!

我是C的新手,并且知道在其他语言中这会起作用,所以任何有关如何做的帮助将非常感谢!

2 个解决方案

#1


2  

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

在没有原型的情况下,您无法访问main之后声明的函数。像这样重写你的代码:

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

int result(float *, float);

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

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

#2


3  

The code is this:`

代码是这样的:`

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

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

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

`

#1


2  

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

在没有原型的情况下,您无法访问main之后声明的函数。像这样重写你的代码:

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

int result(float *, float);

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

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

#2


3  

The code is this:`

代码是这样的:`

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

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

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

`