Re: [問題] 公司考題 (觀念題??)
※ 引述《LinRungChuan (吉他手)》之銘言:
: 這是一家公司給我的題目
: 30 int * someIDs, theFirst, *r;
: 110 someIDs =GetSomeIDs(); /* defined below */
: 111 theFirst = someIDs [0];
: 112 r= ReorderIDs(someIDs);
: 113-150 /* we want to use 'theFirst' and 'r' here*/
: 499 /*-------- GetSomeIDs-----*/
: 500 int * GetSomeIDs()
: 501 {
: 502 int ids[8];
: 503-550 /* The ids are defined here */
: 551 return ids;
: 552 }
: Q1: Is there a different way to write line 500 which preserves
: the same effective prototype? If so, what is it?
: 我回答: Yes, we can replace int * by void GetSomeIDs(int *ids).
: In that case, we don't need to re-declare int ids in line 502
: and no need line 551.
: 結果公司給我個 hint 如下:
: they are looking for an equivalent way to write line #500,
: maintaining the same prototype
: 可是我看完他的hint 我還是不懂他是要我用啥寫法取代 int * GetSomeIDs()...囧
: Q2: What will 'theFirst' contain after line 111 is executed?
: Is this deterministic? Why?
: 這個問題我回答 'theFirst' 是不是 deterministic 要看GetSomeIDs()
: 裡面的寫法才能決定
: 結果公司說the answer is ‘deterministic’要我解釋why....
: 這邊我該如何解釋 'theFirst' 一定是 deterministic?
: (其實我還是覺得不一定是 deterministic....=.=a...)
: ------------------------------------------------------
: 這測試 是那公司的HR寄了 差不多15題的題目給我回答 大概給3天時間
: 可以上網或翻書 查任何需要的資料(但要付上來源)
: 不過不能把題目直接po到網路~XD 他們查到就取消面試資格
: 我還真的有看到 有人把整分題目都po到網路問人...結果公司的人看到
: 在下面回他 他違反他簽的同意書 所以要把他的資格取消....
: 所以基本上我google過了 類似的問題,但還是沒辦法回答他們要的答案..>_<..
個人練習官方說法:
因為區域變數的指標不能回傳
想要得到計算完的id
可以把型態改成 int[8]
用上 typedef
typedef int int8s[8];
int8s GetSomeIDs()
{
int8s ids;
/* The ids are defined here */
return ids8s;
}
不知道是不是你要的答案。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.139.178
→
04/04 07:50, , 1F
04/04 07:50, 1F
→
04/04 07:51, , 2F
04/04 07:51, 2F
→
04/04 07:52, , 3F
04/04 07:52, 3F
→
04/04 07:53, , 4F
04/04 07:53, 4F
→
04/04 07:53, , 5F
04/04 07:53, 5F
推
04/04 11:03, , 6F
04/04 11:03, 6F
→
04/04 11:13, , 7F
04/04 11:13, 7F
→
04/04 12:42, , 8F
04/04 12:42, 8F
int8s 不是陣列 是 結構
推
04/04 15:55, , 9F
04/04 15:55, 9F
→
04/04 15:55, , 10F
04/04 15:55, 10F
→
04/04 15:56, , 11F
04/04 15:56, 11F
→
04/04 15:57, , 12F
04/04 15:57, 12F
→
04/04 16:01, , 13F
04/04 16:01, 13F
→
04/04 16:01, , 14F
04/04 16:01, 14F
沒有打錯 編譯的過(我錯了)
※ 編輯: damody 來自: 140.118.175.32 (04/04 19:05)
推
04/04 19:14, , 15F
04/04 19:14, 15F
→
04/04 19:15, , 16F
04/04 19:15, 16F
→
04/04 19:16, , 17F
04/04 19:16, 17F
真的耶 不能當函數回傳值
→
04/04 20:32, , 18F
04/04 20:32, 18F
推
04/04 21:05, , 19F
04/04 21:05, 19F
→
04/04 21:06, , 20F
04/04 21:06, 20F
→
04/04 21:09, , 21F
04/04 21:09, 21F
真的不是耶,原來還是陣列
換個方法好了,
方法一.
int* GetSomeIDs()
{
static int ids[8];
/* OOXX */
return ids;
}
方法二.
int* GetSomeIDs()
{
int* ids = (int*)malloc(sizeof(int) * 8);
/* OOXX */
return ids;
}
方法一有 多執行緒或是 不能同時呼叫兩次的問題
像是
int* id1 = GetSomeIDs();
int* id2 = GetSomeIDs();
就會錯了
方法二要自己 free 掉
出這種題目可能要加入大量限制
讓解題者的解題方向如預期嗎? > <
※ 編輯: damody 來自: 140.118.175.32 (04/04 21:46)
討論串 (同標題文章)