程式語言 96交通升資 - 考試
By Kumar
at 2014-03-30T23:34
at 2014-03-30T23:34
Table of Contents
[考題] 國考歷屆考題與考題觀念討論(書裡看到的選這個)請附上想法、出處
int *f(int x)
{
int y;
y = x + 10;
return &y;
}
int g(int y)
{
int x;
x = y – 10;
return x;
}
void main( )
{
int *x, y;
x = f(10);
y = g(*x);
printf(“%d %d\n”, *x, y);
}
執行結果為何
小弟認為f() 的return &y是不合理的
y是區域變數lifetime只有到f()結束
回傳給main指定給x不太合理
這樣不就變成dangling pointer
請賜教
謝謝
--
int *f(int x)
{
int y;
y = x + 10;
return &y;
}
int g(int y)
{
int x;
x = y – 10;
return x;
}
void main( )
{
int *x, y;
x = f(10);
y = g(*x);
printf(“%d %d\n”, *x, y);
}
執行結果為何
小弟認為f() 的return &y是不合理的
y是區域變數lifetime只有到f()結束
回傳給main指定給x不太合理
這樣不就變成dangling pointer
請賜教
謝謝
--
Tags:
考試
All Comments
By Todd Johnson
at 2014-04-04T02:02
at 2014-04-04T02:02
By Hazel
at 2014-04-07T07:19
at 2014-04-07T07:19
By Catherine
at 2014-04-08T02:33
at 2014-04-08T02:33
By Irma
at 2014-04-09T04:05
at 2014-04-09T04:05
By William
at 2014-04-13T02:29
at 2014-04-13T02:29
By Kumar
at 2014-04-13T06:11
at 2014-04-13T06:11
Related Posts
五月關島考試
By Madame
at 2014-03-30T21:32
at 2014-03-30T21:32
民訴家事事件法
By Kyle
at 2014-03-30T19:25
at 2014-03-30T19:25
行政罰法問題
By Xanthe
at 2014-03-30T18:06
at 2014-03-30T18:06
公共管理函授師資
By Dorothy
at 2014-03-30T17:29
at 2014-03-30T17:29
當有人買炸物來圖書館…
By Isabella
at 2014-03-30T15:31
at 2014-03-30T15:31