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,并编写该程序以首先读取值并将它们保存到大小为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 -lm
stuff or it wouldn't compile and said something like pow not found or so)?
由于这是我第一次编程,我真的不知道我的代码有什么问题。我是否必须为编译器或类似的东西设置更多的标志(因为我必须这样做-lm东西或它不会编译并说像找不到的东西等)?
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位,如果它是操作系统的问题。
2 个解决方案
#1
7
werte[]
and nul[]
are arrays, but the word werte
itself is an address of the first element of the array. So when you do &werte
you'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 werte
and nul
without the ampersand signs to pass the address of these arrays.
werte []和nul []是数组,但是werte这个词本身就是数组第一个元素的地址。所以当你做&werte时你试图传递一个地址的地址(正如@cnicutar指出的那样,这应该实际上是读取指向N个元素数组的指针)。所以只是通过werte和nul没有符号标志来传递这些数组的地址。
#2
11
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 FFT
expects. FFT expects a pointer to a double and &werte
produces a pointer to an array of N elements. So, in order to make FFT
happy, just pass werte
which 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 FAQ。
#1
7
werte[]
and nul[]
are arrays, but the word werte
itself is an address of the first element of the array. So when you do &werte
you'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 werte
and nul
without the ampersand signs to pass the address of these arrays.
werte []和nul []是数组,但是werte这个词本身就是数组第一个元素的地址。所以当你做&werte时你试图传递一个地址的地址(正如@cnicutar指出的那样,这应该实际上是读取指向N个元素数组的指针)。所以只是通过werte和nul没有符号标志来传递这些数组的地址。
#2
11
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 FFT
expects. FFT expects a pointer to a double and &werte
produces a pointer to an array of N elements. So, in order to make FFT
happy, just pass werte
which 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 FAQ。