看板 Soft_Job 關於我們 聯絡資訊
我也來拆拆看 : 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)
hilorrk:typedef int(*RPZ)(int); 06/06 22:00
hilorrk:typedef RPZ (*PZ)(char); 06/06 22:01
hilorrk:typedef int (*RRZ)(int); 06/06 22:01
hilorrk:typedef RRZ (*RZ)(char); 06/06 22:01
hilorrk:typedef RZ (*Z)(PZ); 06/06 22:01
yauhh:這個函數要怎麼使用啊? 在宣告中認得自己的z和f,而如果要把 06/07 00:39
yauhh:這個指定給a,那a要怎麼宣告? 06/07 00:42
avhacker:http://goo.gl/1GDy0 06/07 01:15
avhacker:說實在,這種東西沒啥意義,對面試更沒有幫助 06/07 01:16
avhacker:他可能不知道要怎麼樣找人,只好找(某些方面)像自己的人 06/07 01:16
ianlin45:好駭客未必是好領導 06/07 07:56
dryman:其實就是要懂C spec.. 06/07 13:18
bobju:原文當中就有提到了:他知道這很沒營養很無聊,但他的重點是要 06/07 14:04
bobju:讓人知道他們對技術的追求很瘋狂很執著.所以才會出這道怪題. 06/07 14:04
avhacker:要讓人知道有很多方法. 考題是做過濾用的,這是誤用 06/07 15:37
avhacker:哦不對,那好像不是考題 XD 06/07 15:38
avhacker:不過搞那還是沒啥意義,不如讀我前面貼的那篇 06/07 15:39
yauhh:我覺得,寫一個函數就是要拿來用.這行寫了一個二層的函數指標 06/08 10:07
yauhh:很好,很酷,但是酷了大半天只是人們都在討論如何閱讀它,而不 06/08 10:07
yauhh:談怎麼呼叫它,使用它,那當然沒意義了. 06/08 10:08
wendly777:其實這些考題只是要找到一個認真鑽研過某技術的人 06/08 23:24
wendly777:這題根本無所謂實用性..只是要看有沒有認真研究過 06/08 23:29
wendly777:另外CIH的面試很活..他總是可以鑑別出怪物RD 06/08 23:32
wendly777:不同的人會有不同的問法..每個人領域/專長都不同 06/08 23:33
wendly777:有被他面試過的應該都知道.... 06/08 23:33
yauhh:我不認為它沒有實用性.這是CIH自己曾經寫過的程式,沒實用? 06/09 10:14
omnl:好奇的問一下 理論上怪物級leading加上他能找到一群怪物級RD 06/09 12:59
omnl:幾年下來應該也可期待一些怪物作品出來 但是好像都沒耳聞到 06/09 13:00
HowLeeHi:他那時在網虎說有改出全世界最小的linux kernel 06/09 17:37