我如何读取C这个复杂的声明? [英] How do I read this complex declaration in C?

查看:82
本文介绍了我如何读取C这个复杂的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:

  <一href=\"http://stackoverflow.com/questions/3706704/whats-the-meaning-of-this-piece-of-$c$c-void-signalint-sig-void-funcin\">what's这块code的含义?无效(*信号(INT SIG,无效(* FUNC)(INT)))(int);在

我已经从signal.h中头文件,采取了复杂的声明,以下是声明。

I have a complex declaration which have been taken from the "signal.h" header file, and below is the declaration.

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

现在,我该如何解析呢?为

Now, how do I parse it? As

信号的功能以两个参数的int类型的'签名'和'功能',这是一个指向函数回吐INT作为参数,返回void类型;它返回一个函数指针回吐INT作为参数,返回void。

signal is function taking two arguments ‘sig’ of int type and ‘func’, which is a pointer to a function taking int as an argument and returns void type; it returns a pointer to the function taking int as argument and returning void.

它是确定或信号的功能的指针?

Is it OK or signal is a pointer to function?

推荐答案

先从最左边的标识符和工作的方式了,记住, [] ()绑定之前 * ,所以 *一个[] 是数组指针,(*一)[] 是一个指向数组的指针, * F()是一个返回指针和(* F)()是指向一个功能:

Start with the leftmost identifier and work your way out, remembering that [] and () bind before *, so *a[] is an array of pointers, (*a)[] is a pointer to an array, *f() is a function returning a pointer, and (*f)() is a pointer to a function:

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

信号相关联的信号处理函数 FUNC 与信号 SIG ,并将指针返回到老的信号处理函数:

signal associates a signal handler function func with a signal sig, and returns the pointer to the old signal handler function:

void new_interrupt_handler(int sig)
{
  ... // do something interesting with interrupt signal
}

int main(void)
{
  void (*old_interrupt_handler)(int);
  ...
  /**
   * Set up our new interrupt handler
   */
  old_interrupt_handler = signal(SIGINT, new_interrupt_handler);
  ...
  /**
   * Restore original interrupt handler
   */
  signal(SIGINT, old_interrupt_handler);
  ...
}

这篇关于我如何读取C这个复杂的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆