[問題] struct 內傳值的問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VS2005
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
NO
問題(Question):
給一組LLdouble的struct
f1是傳一個變數讓他改
f2是傳一個值出來
但為什麼f1改的值一直不正確?
餵入的資料(Input):
No
預期的正確結果(Expected Output):
LD f1 1.00000
LD f2 1.00000
錯誤結果(Wrong Output):
LD f1 0.00000
LD f2 1.00000
程式碼(Code):(請善用置底文網頁, 記得排版)
typedef struct LLdouble{
double numberStr;
} LLdouble;
//以下這個函數不能工作
void f1(double a2,LLdouble a1){
//這裡printf(a1)為 0
a1.numberStr=a2;
//這裡printf(a1)為 0
}
//這個函數是可以工作的
LLdouble f2(double a2){
LLdouble a1;
//這裡printf(a1)為 0 (運氣?)
a1.numberStr=a2;
//這裡printf(a1)為 1.00000
return a1;
}
//main
void main()
{
LLdouble LD;
f1(1.0,LD);
printf("LD f1 %f \n",LD.numberStr);//這裡出現0應該要出現1的
LD=f2(1.0);
printf("LD f2 %f \n",LD.numberStr);
system("pause");/*暫停功能*/
}
結果:
LD f1 0.00000
LD f2 1.00000
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.63.132
→
06/17 22:01, , 1F
06/17 22:01, 1F
加上去了
→
06/17 22:01, , 2F
06/17 22:01, 2F
l大 不好意思 這句我有看沒有懂= =
※ 編輯: wope 來自: 140.112.63.132 (06/17 22:07)
→
06/17 22:13, , 3F
06/17 22:13, 3F
→
06/17 22:14, , 4F
06/17 22:14, 4F
→
06/17 22:15, , 5F
06/17 22:15, 5F
感謝love大提供方向
不過想跟love大再確定看看這想做是否正確
我這樣作是不是直對記憶體的值做修改?
//以下這個函數目前跑出來的值是這確的
void f1(double a2,LLdouble &a1){
//這裡printf(a1)為 0
a1.numberStr=a2;
//這裡printf(a1)為 1.00000
}
不知道這樣寫是不是合法的寫法?
※ 編輯: wope 來自: 140.112.63.132 (06/17 22:26)
→
06/17 22:28, , 6F
06/17 22:28, 6F
→
06/17 22:33, , 7F
06/17 22:33, 7F
推
06/26 11:06, , 8F
06/26 11:06, 8F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章