这两个参数在typedef中意味着什么?

时间:2015-02-18 15:31:36

标签: c function typedef

我有一段代码,我不明白那个typedef

typedef void (inst_cb_t) (const char*, size_t);

这是否真的意味着您现在可以将inst_cb_t用作void?但是第二个括号中的内容呢?

1 个答案:

答案 0 :(得分:11)

声明

typedef void (inst_cb_t) (const char*, size_t);

inst_cb_t定义为一个函数,它接受const char*size_t类型的两个参数并返回void

此声明的一个有趣部分是,您只能在函数声明和指向函数减速的指针中使用它。

inst_cb_t foo;  

您无法在

等功能定义中使用它
inst_cb_t foo      // WRONG
{
    // Function body
}  

看看C标准:

C11:6.9.1函数定义:

  

函数定义中声明的标识符(函数的名称)应具有函数类型,由函数定义的声明符部分指定。 162

和脚注162是

  

意图是函数定义中的类型类别不能从typedef继承:

typedef int F(void);              // type F is ‘‘function with no parameters
                                  // returning int’’
F f, g;                           // fand g both have type compatible with F
F f { /* ... */ }                 // WRONG: syntax/constraint error
F g() { /* ... */ }               // WRONG: declares that g returns a function
int f(void) { /* ... */ }         // RIGHT: f has type compatible with F
int g() { /* ... */ }             // RIGHT: g has type compatible with F
F *e(void) { /* ... */ }          // e returns a pointer to a function
F *((e))(void) { /* ... */ }      // same: parentheses irrelevant
int (*fp)(void);                  // fp points to a function that has type F
F*Fp;                             // Fp points to a function that has type F