[課業] C++ 程式指標 疑問

題目出處:第4次全國各級農會-新進九職等以下-程式設計 (填充題 第3題) https://i.imgur.com/qoZ2n9x.png 題目: #include using namespace std; int func(int x,int& y,int *z) { x=x+1; y=y+2; z=&x; retuen x+y+*z; //我算出來的x=3,y=6,*z=3,與答案相同 } int main() { int a=2,b=4,c=6; printf("ANS1=%d",func(a,b,&c)); //我算出來的a=2,b=6,c=3,答案為a=2,b=6,c=6 printf("\ANS2=%d",a+b+c); return 0; } 答案(執行的輸出結果): ANS1=12 ANS2=14 我算出來得答案為: ANS1=12 ANS2=11 疑問點: 傳參考和傳址,不是都共用同一塊記憶體嗎? 為何 y=y+2; ,會更改到 b 的值, 而 z=&x; , c 的值還是維持在原本的 6 ? https://i.imgur.com/c1i007A.png --
QR Code
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 192.83.186.60
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Examination/M.1525573003.A.7C7.html※ 編輯: usisiu (192.83.186.60), 05/06/2018 10:17:05

Let's block ads! (Why?)