C语言 警告:传递参数“来自不兼容的指针类型[默认启用]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14073870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:54:06  来源:igfitidea点击:

warning: passing argument ’from incompatible pointer type [enabled by default]'

cpointersfft

提问by mitit100

I've been looking around on the other threads somehow related to this, but somehow I just don't get it...

我一直在寻找与此相关的其他线程,但不知何故我只是不明白......

I want to do some FFT on a set of values I have evaluated and wrote this program to first read the values and save them to an array of size n.

我想对我已经评估过的一组值做一些 FFT 并编写这个程序来首先读取这些值并将它们保存到 size 的数组中n

int main () {
    // some variables and also a bit of code to read the 'messwerte.txt'

printf("Geben sie an wieviele Messwerte ausgelesen werden sollen: ");
scanf("%d", &n);
double werte[n]; //Array der "fertigen" Messwerte
in = fopen ("messwerte.txt","r");
double nul[n]; //Array von nullen

int logN = 14;
l=FFT(logN,&werte,&nul);
}

In the same file I also do the FFT with the help of this program:

在同一个文件中,我也在这个程序的帮助下做 FFT:

double FFT (int logN, double *real, double *im) //logN is base 2 log(N) {
// blabla FFT calculation
}

However, when I compile I always get this error:

但是,当我编译时,我总是收到此错误:

gcc FFT.c -lm
FFT.c: In function ‘main':
FFT.c:94:2: warning: passing argument 2 of ‘FFT' from incompatible pointer type [enabled by default]
FFT.c:4:8: note: expected ‘double *' but argument is of type ‘double (*)[(unsigned int)(n)]'
FFT.c:94:2: warning: passing argument 3 of ‘FFT' from incompatible pointer type [enabled by default]
FFT.c:4:8: note: expected ‘double *' but argument is of type ‘double (*)[(unsigned int)(n)]'

Since this is my first time programming, I really don't know what is wrong with my code. Will I have to set more flags for the compiler or stuff like that (because I had to do this -lmstuff or it wouldn't compile and said something like pow not found or so)?

由于这是我第一次编程,我真的不知道我的代码有什么问题。我是否必须为编译器或类似的东西设置更多标志(因为我必须做这些-lm东西,否则它不会编译并说诸如 pow not found 之类的东西)?

Also I was made aware that there might be a difference when writing on a Windows or a Linux machine, and I am using Linux, Lubuntu 12.10 32-bit if it's a problem of the OS.

我也意识到在 Windows 或 Linux 机器上编写代码可能会有所不同,如果是操作系统的问题,我使用的是 Linux、Lubuntu 12.10 32 位。

回答by cnicutar

l=FFT(logN,&werte,&nul);
           ^      ^
l=FFT(logN,&werte,&nul);
           ^      ^

Drop ampersands from that line.

从该行删除&符号。



The problem is that the &operator in this context produces an expression with a different type than what FFTexpects. FFT expects a pointer to a double and &werteproduces a pointer to an array of N elements. So, in order to make FFThappy, just pass wertewhich will quietly decay to a pointer to the first element.

问题是&此上下文中的运算符生成的表达式与FFT预期的类型不同。FFT 需要一个指向 double 的指针,并&werte生成一个指向 N 个元素数组的指针。所以,为了让自己FFT开心,只需要传递werte一个会悄悄衰减到第一个元素的指针。

For more information on pointers to arrays, there's a C FAQ.

有关指向数组的指针的更多信息,请参阅C 常见问题解答

回答by varevarao

werte[]and nul[]are arrays, but the word werteitself is an address of the first element of the array. So when you do &werteyou're trying to pass an address of the address (as @cnicutar pointed out, this should actually read pointer to an array of N elements). SO just pass werteand nulwithout the ampersand signs to pass the address of these arrays.

werte[]nul[]是数组,但单词werte本身是数组第一个元素的地址。因此,当您这样做时,您&werte试图传递地址的地址(正如@cnicutar 指出的那样,这实际上应该读取指向 N 个元素数组的指针)。所以只需传递werte并且nul没有&符号来传递这些数组的地址。