我正在嘗試在int testfn下面運行一個函式,它應該為下面的指標(指向一個復數)分配一個復數double _Complex *foo,它被饋送。最小(非作業)示例 .c 檔案test.c如下。
當我呼叫testfnthrough 時main,它不會回傳分配的值1.0 0.5*I,而是回傳0.0 0.0*I. 我不明白為什么,或者如何解決它。
測驗.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
int testfn(double _Complex *foo) {
double _Complex foobar = 1.0 0.5*I;
foo = &foobar;
printf("foobar = %.8f I %.8f \n",creal(foobar),cimag(foobar));
printf("*foo = %.8f I %.8f \n",creal(*foo),cimag(*foo));
return 0;
}
int main() {
double _Complex toot;
testfn(&toot);
printf("toot = %.8f I %.8f \n",creal(toot),cimag(toot));
return 0;
}
編譯并運行
cc -o test test.c -L. -lm -Wall
./test
終端輸出給出錯誤答案
foobar = 1.00000000 I 0.50000000
*foo = 1.00000000 I 0.50000000
toot = 0.00000000 I 0.00000000
請注意, 的實際答案toot應該是toot = 1.00000000 I 0.50000000。
另一方面,這個陣列案例有效
在這里,在 中test2.c,我正在饋送double _Complex (*foo)[3],即一個指向復數的三維指標陣列(對嗎?)到int testarrayfn,然后指定foo為一個特定的 2x3 復數矩陣testarrayfn。
當我打電話testarrayfn從main使用double _Complex toot[2][3];和testarrayfn(toot);,我得到正確的答案。我不確定為什么這樣做是正確的(我只是在谷歌上搜索并使用陣列進行了很多操作,并傳遞了陣列引數)。
測驗2.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
int testarrayfn(double _Complex (*foo)[3]) {
double _Complex foobar;
int i,j;
for (i=0;i<2;i ) {
for (j=0;j<3;j ) {
foobar = 1.*(i 1) 0.5*(j 1)*I ;
foo[i][j] = foobar;
printf("[%d][%d]: foobar = %.8f I %.8f \n",i,j,creal(foobar),cimag(foobar));
printf("foo[%d][%d] = %.8f I %.8f \n",i,j,creal(foo[i][j]),cimag(foo[i][j]));
}
}
return 0;
}
int main() {
double _Complex toot[2][3];
testarrayfn(toot);
int i,j;
for (i=0;i<2;i ) {
for (j=0;j<3;j ) {
printf("toot[%d][%d] = %.8f I %.8f \n",i,j,creal(toot[i][j]),cimag(toot[i][j]));
}
}
return 0;
}
編譯并運行
cc -o test2 test2.c -L. -lm -Wall
./test2
終端輸出給出了預期的答案
[0][0]: foobar = 1.00000000 I 0.50000000
foo[0][0] = 1.00000000 I 0.50000000
[0][1]: foobar = 1.00000000 I 1.00000000
foo[0][1] = 1.00000000 I 1.00000000
[0][2]: foobar = 1.00000000 I 1.50000000
foo[0][2] = 1.00000000 I 1.50000000
[1][0]: foobar = 2.00000000 I 0.50000000
foo[1][0] = 2.00000000 I 0.50000000
[1][1]: foobar = 2.00000000 I 1.00000000
foo[1][1] = 2.00000000 I 1.00000000
[1][2]: foobar = 2.00000000 I 1.50000000
foo[1][2] = 2.00000000 I 1.50000000
toot[0][0] = 1.00000000 I 0.50000000
toot[0][1] = 1.00000000 I 1.00000000
toot[0][2] = 1.00000000 I 1.50000000
toot[1][0] = 2.00000000 I 0.50000000
toot[1][1] = 2.00000000 I 1.00000000
toot[1][2] = 2.00000000 I 1.50000000
有關的其他問題 complex.h
我在任何地方都找不到以下問題的答案:
- 可以在函式和引數中交替使用
double complex foo和double _Complex foo嗎? - 可以在函式和引數中交替使用
double complex *foo和double _Complex *foo嗎? - 可以在函式和引數中交替使用
double complex (*foo)[3]和double _Complex (*foo)[3]嗎? - 更一般地,有沒有什么區別
double complex,double _Complex和complex double?
uj5u.com熱心網友回復:
測驗1
兩個問題。
foo是區域變數。當您分配它時,它僅在函式范圍內有效。foobar是區域變數,函式回傳后無法訪問(因為它停止存在)
您需要foobar在函式回傳后使存在并將參考傳遞給指標。
int testfn(double _Complex **foo) {
static double _Complex foobar = 1.0 0.5*I;
*foo = &foobar;
printf("foobar = %.8f I %.8f \n",creal(foobar),cimag(foobar));
printf("*foo = %.8f I %.8f \n",creal(**foo),cimag(**foo));
return 0;
}
int main() {
double _Complex *toot;
testfn(&toot);
printf("toot = %.8f I %.8f \n",creal(*toot),cimag(*toot));
return 0;
}
或使用foobar(非參考)的值分配底層物件
int testfn(double _Complex *foo) {
double _Complex foobar = 1.0 0.5*I;
*foo = foobar;
printf("foobar = %.8f I %.8f \n",creal(foobar),cimag(foobar));
printf("*foo = %.8f I %.8f \n",creal(*foo),cimag(*foo));
return 0;
}
int main() {
double _Complex toot;
testfn(&toot);
printf("toot = %.8f I %.8f \n",creal(toot),cimag(toot));
return 0;
}
Test2 作業是因為您將值分配給參考的物件,而不是嘗試更改指向陣列的指標。
您可以撰寫foo[i][j] = foobar;as *(*(foo i) j) = foobar;,它與我的第二個示例相同(您可以將其toot視為單元素陣列)
_Complex是來自 C99(或 C99 之前的 GCC 擴展)的實際 C 關鍵字。complex是一個擴展到 的簡單定義_Complex。這樣做是因為許多舊程式使用自己的complex型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/402629.html
標籤:
