2013-04-01 42 views
3

幫我理解下面的原型。 (int)在做什麼?如何閱讀這個原型?

void (*signal(int sig, void (*handler)(int))) (int); 
+1

退房[這個答案](http://stackoverflow.com/a/1591492/1383051)。它非常詳細地回答你的問題! –

+0

可能的重複[瞭解C中函數指針的typedef:請參考示例,提示和技巧](http://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c-examples- hints-and-tips-ple) –

+0

@JimBalter,不太確定這是重複的。這個問題旨在理解函數指針'typedef's。這個問題旨在理解函數指針聲明。這個問題的答案只是回答了這兩個問題。但是,我可以理解,如果人們已經把答案作爲答案來解決問題。 –

回答

0

void (*handler)(int);處理程序是指向函數(比如Fn1的)配有返回類型void和接受一個int

void (*signal(int sig, Fn1)) (int);

(*signal(int sig, Fn1);這個函數有返回類型void和接受一個intfunction pointer作爲參數。

的返回類型此功能,它的指針是signal可能是一個函數的函數指針需要一個int [最後int]返回類型爲void

8

整件事聲明瞭一個名爲signal功能:

  • signal採用int和一個函數指針
    • 此函數指針需要一個int並返回void
  • signal返回一個函數指針
    • 這個函數指針需要一個int,並返回一個void

這就是最後int進來。


您可以使用the spiral rule來理解這些聲明或程序cdecl(1)

+1

+1來通知'cdecl'和[spiral rule](http://c-faq.com/decl/spiral.anderson.html)。看起來像一個很棒的工具 –

0

以上的原型現在可以寫成::

typedef void (*sig_t) (int); 

sig_t signal(int sig, sig_t handler); 

,我希望這將是明確的給你。

0

它返回一個指向函數返回void..Source一個功能:在深度C祕密

+0

一個側面說明:如果你讀過_Deep C Secrets_,請記住,這本書是非常古老的,它的大部分咆哮都是無效的,即使在C89。 – Shahbaz

2

正如我在回答中指出,到最近的另一項問題「解讀C聲明」,一個辦法理解這些聲明是將參數列表和數組聲明與左邊的東西交換,然後向後讀取聲明。在這種情況下,讓你

void (*signal(int sig, void (*handler)(int))) (int) 

- >

void (int)(*(int sig, void (int)(*handler))signal) 

其內容爲「'信號是一個函數,它有兩個參數,並返回一個指針,接受一個int參數,並返回void的函數」。這兩個參數是一個int(sig)和一個指針(處理函數),它接受一個int參數並返回void。

或者你可以做精神交換,這與螺旋規則相同。

+0

很高興我再次upvote這個答案:D –

4

找到最左邊的標識符並找出您的出路,記住[]()*之前綁定; IOW,*a[]是指針數組,(*a)[]是指向數組的指針,*f()是返回指針的函數,而(*f)()是指向函數的指針。因此,

void (*signal(int sig, void (*handler)(int))) (int); 

擊穿作爲

 signal           -- signal 
     signal(        )   -- is a function 
     signal( sig      )   -- with a parameter named sig 
     signal(int sig,      )   -- of type int 
     signal(int sig,  handler  )   -- and a parameter named handler 
     signal(int sig,  *handler  )   -- which is a pointer 
     signal(int sig,  (*handler)( )))   -- to a function 
     signal(int sig,  (*handler)(int)))   -- taking an int parameter 
     signal(int sig, void (*handler)(int)))   -- and returning void 
     *signal(int sig, void (*handler)(int)))   -- returning a pointer 
    (*signal(int sig, void (*handler)(int)))( ) -- to a function 
    (*signal(int sig, void (*handler)(int)))(int) -- taking an int parameter 
void (*signal(int sig, void (*handler)(int)))(int); -- and returning void 

signal功能關聯與回調函數handler)的信號(sig),像這樣:

#include <signal.h> 

static int interrupt = 0; 

/** 
* The following function will be called when a SIGINT is 
* detected (such as when someone types Ctrl-C) 
*/ 
void interrupt_handler(int sig) 
{ 
    interrupt = 1; 
} 

int main(void) 
{ 
    /** 
    * Declare a pointer to the old interrupt handler function 
    */ 
    void (*old_interrupt_handler)(int); 

    /** 
    * Save the old interrupt handler while setting the new one 
    */ 
    old_interrupt_handler = signal(SIGINT, interrupt_handler); 
    while (!interrupt) 
    { 
    // do stuff until someone hits Ctrl-C 
    }; 

    /** 
    * restore the original interrupt handler 
    */ 
    signal(SIGINT, old_interrupt_handler); 
    return 0; 
} 
+0

作爲人們遇到這個答案的旁註:[你可能會'sigaction'而不是'signal'](http://stackoverflow.com/a/232711/ 912144)。 – Shahbaz