Re: [閒聊] 十年前CIH怪物團隊的面試題目...
我也來拆拆看
: int (*(*z(int (*(*f)(char))(int)))(char))(int)
: {
: }
最外層包的是function pointer to function(int) reutrn int
剩下(*z(int (*(*f)(char))(int)))(char)
再拆:z is a pointer to a funciton that take [int (*(*f)(char))(int))]
as parameter and return a pointer to function that take [char]
and return a pointer to a function that take [int]
and return int
int (*(*f)(char))(int) 就更簡單了
f is a pointer to a function that take [char]
and return a pointer to a function that take [int]
and return int
C 的function語意設計很奇怪
它parse的層級優先度是:
括號最上級
postfix括號們(),[]第二級
pointer一般原則是修飾「左邊」的元素,除非自己是在式子最左邊
或在括號內的最左邊,則是修飾括號外的東西
type qualifier const也是修飾「左邊」為主,除非自己在式子最右邊
所以說一般的 function pointer要這麼讀
int const (*ABC)(int)
ABC is a pointer to function(int) that return a const int
int * const (*ABC)(int)
ABC is a pointer to function(int) that return a const pointer to int
int * const * (*ABC)(int)
ABC is a pointer to function(int) that return a pointer to const pointer to int
ABC在最裡面,他後面沒東西(沒有[])
所以開始往左看,看到一個pointer
所以他是一個pointer to...
pointer被括號括起來,所以就是point到外面的東西
外面是int const {...} (int)
這時要先從右邊括號開始讀
所以是一個function吃int並且回傳{int const}
{int const}也是從右邊讀到左邊(記得:cosnt, pointer都是修飾左邊的東西!)
所以是function吃int並回傳const的int
如果加上[]的話就會更複雜了
int * const (*ABC[3])(int)
記得:有後置括號時先讀後置括號
ABC is a array of 3 pointer to funciton(int) to const pointer int
由於 C 不能回傳一個 function,只能回傳function pointer
所以程式很容易就變得很難讀...
: 雖然我應該會用 typedef 來處理這麼複雜的宣告,
: 但還是想把這個看懂:
: 先簡化這個 function 把 parameter 拿掉
: int (*(*z())(char))(int)
: parameter:
: int (*(*f)(char))(int)
: 這應該簡單不少, 所以參數是
: 一個 function pointer, 傳入參數 type 是 char, 傳回值 type 是
: 一個 function pointer, 傳入參數 type 是 int, 傳回值 type 是 int
: 再來看 function z 本身:
: int (*(*z())(char))(int)
: function z 傳回值 type:
: 一個 function pointer, 傳入參數 type 是 char, 傳回值 type 是
: 一個 function pointer, 傳入參數 type 是 int, 傳回值 type 是 int
: 再麻煩大家看看有沒有寫錯的部份。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 71.20.147.248
※ 編輯: dryman 來自: 71.20.147.248 (06/06 21:08)
→
06/06 22:00, , 1F
06/06 22:00, 1F
→
06/06 22:01, , 2F
06/06 22:01, 2F
→
06/06 22:01, , 3F
06/06 22:01, 3F
→
06/06 22:01, , 4F
06/06 22:01, 4F
→
06/06 22:01, , 5F
06/06 22:01, 5F
推
06/07 00:39, , 6F
06/07 00:39, 6F
→
06/07 00:42, , 7F
06/07 00:42, 7F
推
06/07 01:15, , 8F
06/07 01:15, 8F
→
06/07 01:16, , 9F
06/07 01:16, 9F
→
06/07 01:16, , 10F
06/07 01:16, 10F
推
06/07 07:56, , 11F
06/07 07:56, 11F
→
06/07 13:18, , 12F
06/07 13:18, 12F
推
06/07 14:04, , 13F
06/07 14:04, 13F
→
06/07 14:04, , 14F
06/07 14:04, 14F
推
06/07 15:37, , 15F
06/07 15:37, 15F
→
06/07 15:38, , 16F
06/07 15:38, 16F
→
06/07 15:39, , 17F
06/07 15:39, 17F
→
06/08 10:07, , 18F
06/08 10:07, 18F
→
06/08 10:07, , 19F
06/08 10:07, 19F
→
06/08 10:08, , 20F
06/08 10:08, 20F
推
06/08 23:24, , 21F
06/08 23:24, 21F
→
06/08 23:29, , 22F
06/08 23:29, 22F
→
06/08 23:32, , 23F
06/08 23:32, 23F
→
06/08 23:33, , 24F
06/08 23:33, 24F
→
06/08 23:33, , 25F
06/08 23:33, 25F
→
06/09 10:14, , 26F
06/09 10:14, 26F
→
06/09 12:59, , 27F
06/09 12:59, 27F
→
06/09 13:00, , 28F
06/09 13:00, 28F
推
06/09 17:37, , 29F
06/09 17:37, 29F
討論串 (同標題文章)
Soft_Job 近期熱門文章
32
103
PTT職涯區 即時熱門文章
77
173