C++ 程式指標 疑問 - 考試
By Edwina
at 2018-05-06T10:16
at 2018-05-06T10:16
Table of Contents
題目出處:第4次全國各級農會-新進九職等以下-程式設計 (填充題 第3題)
https://i.imgur.com/qoZ2n9x.png
題目:
#include <iostream>
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
--
Tags:
考試
All Comments
By David
at 2018-05-10T15:07
at 2018-05-10T15:07
By Harry
at 2018-05-13T21:54
at 2018-05-13T21:54
By Xanthe
at 2018-05-18T14:53
at 2018-05-18T14:53
By Hazel
at 2018-05-19T09:33
at 2018-05-19T09:33
By Doris
at 2018-05-23T23:29
at 2018-05-23T23:29
By Lily
at 2018-05-25T14:25
at 2018-05-25T14:25
By Yedda
at 2018-05-26T07:36
at 2018-05-26T07:36
By Gary
at 2018-05-28T01:39
at 2018-05-28T01:39
By Caroline
at 2018-06-01T23:39
at 2018-06-01T23:39
Related Posts
102年司法官刑訴
By Elizabeth
at 2018-05-06T03:23
at 2018-05-06T03:23
林蕙真中會新論第九版適用明年考試嗎
By David
at 2018-05-06T01:47
at 2018-05-06T01:47
關於調查局特考兵役問題
By Isla
at 2018-05-05T23:58
at 2018-05-05T23:58
郵局口試消除緊張
By Delia
at 2018-05-05T17:59
at 2018-05-05T17:59
請問非本科電機技師應考資格
By Adele
at 2018-05-05T14:48
at 2018-05-05T14:48