所以我想要一個名為 poly_el 的結構,它存盤多項式元素的系數和冪的值(例如,3x^4 將在結構中存盤為 3 和 4)。我當然希望這些是 double 型別。最終,我希望制作一個此類元素的鏈表來表示整個多項式。所以我使用指向結構的指標,出于某種原因,指標只回傳 0 而不是我分配給它的值。
這是代碼的要點:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
struct poly_el {
double coeff;
double power;
struct poly_el *next;
};
int main() {
double a=10.0;
double b=20.0;
struct poly_el *spe;
spe=(struct poly_el *)malloc(sizeof(struct poly_el));
spe->coeff=a;
spe->power=b;
printf("%f coeff, %f power", &spe->coeff, &spe->power);
}
我希望它輸出 10 coeff,20 power,但它只輸出 0.000。另外,我嘗試過 %lf,%ld 而不是 %f ,也嘗試使用浮點數執行相同的代碼。這些似乎都沒有奏效。我覺得我在分配 a 和 b spe->coeff 和 power 時存在某種錯誤。
uj5u.com熱心網友回復:
問題是您通過參考變數 spe->coeff 和 spe->power,而您想列印這些值,所以只需在您的 printf 中去掉符號 & ,例如:
printf("%f coeff, %f power", spe->coeff, spe->power);
請記住,通過參考指向變數會為您提供該變數在記憶體中的地址。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384367.html
上一篇:C獲取指標的型別
下一篇:如何在C中復制雙指標陣列?
