C 带括号的typedef像什么;typedef int(f)(无效)“;什么意思?它是一个功能原型吗?
这里的C 带括号的typedef像什么;typedef int(f)(无效)“;什么意思?它是一个功能原型吗?,c,typedef,C,Typedef,这里的fc\u name是任何有效的C符号 这与函数指针typedef有多大不同?第一个括号是多余的-它相当于: typedef int (fc_name) (void); 我不认为这有什么用处,尽管我不能让GCC自己抱怨它 这意味着fc_name是不带参数并返回int的函数类型的别名。虽然您可以使用以下命令声明rand() typedef int fc_name(void); 不能在函数定义中使用typedef 指向函数typedef的指针应为: fc_name rand; 此代码表明
fc\u name
是任何有效的C符号
这与函数指针
typedef
有多大不同?第一个括号是多余的-它相当于:
typedef int (fc_name) (void);
我不认为这有什么用处,尽管我不能让GCC自己抱怨它
这意味着fc_name
是不带参数并返回int
的函数类型的别名。虽然您可以使用以下命令声明rand()
typedef int fc_name(void);
不能在函数定义中使用typedef
指向函数typedef的指针应为:
fc_name rand;
此代码表明,不带星号的typedef不是函数指针(处理现在已删除的备选答案):
在编译时,“gcc”表示:
static int function(void)
{
return 0;
}
typedef int fc_name1 (void);
typedef int (fc_name2)(void);
typedef int (*fc_name3)(void);
fc_name1 x = function;
fc_name2 y = function;
fc_name3 z = function;
这段代码表明您确实可以使用fc_name*var=funcname代码>如以下建议所示:
并且,根据以下人士的评论:
静态int函数(void)
{
返回0;
}
typedef int fc_name1(无效);
typedef int(fc_name2)(无效);
typedef int(*fc_name3)(无效);
fc_name1 x_0=函数;//错误
fc_name1*x_1=函数;//x_1是指向函数的指针
fc_name1 x_2;//宣布int x_2(无效);
fc_name 1*x_3=x_2;//声明用x_2初始化x_3
fc_name2 y_0=函数;//该死的贝塞尔函数-没有
fc_name 2*y_1=函数;//该死的贝塞尔函数-没有
fc_名称1 y_2;//宣布int y_2(无效);
fc_name 1*y_3=x_2;//声明y_3已用y_2初始化
fc_name3 z=函数;
有趣-C的暗角确实很模糊。它是函数类型的typedef
。其目的是将其用于函数指针,但在本例中,使用它的语法是:
static int function(void)
{
return 0;
}
typedef int fc_name1 (void);
typedef int (fc_name2)(void);
typedef int (*fc_name3)(void);
fc_name1 x_0 = function; // Error
fc_name1 *x_1 = function; // x_1 is a pointer to function
fc_name1 x_2; // Declare int x_2(void);
fc_name1 *x_3 = x_2; // Declare x_3 initialized with x_2
fc_name2 y_0 = function; // Damn Bessel functions - and no <math.h>
fc_name2 *y_1 = function; // Damn Bessel functions - and no <math.h>
fc_name1 y_2; // Declare int y_2(void);
fc_name1 *y_3 = x_2; // Declare y_3 initialized with y_2
fc_name3 z = function;
更新:
如注释中所述,typedef
可用于声明函数。一个用途是声明一组回调函数:
int bar(void);
fc_name* foo = bar; /* Note the * */
所以我所问的typedef(这里第4行)代表一个
函数类型和与函数指针类型定义不同。
这种类型的定义没有太大意义。它们被用作
样式标准或只是有意创建混淆;-) 正确的形式是:
test_func1 called
test_func2 called
test complete
您可以定义如下所示的函数:
typedef int (*myfunc)(void);
然后定义此函数的变量点:
int helloword(void) {
printf("hello, world\n");
}
并通过函数指针调用函数:
myfunc hw_func;
hw_func = helloworld;
我们需要函数指针的原因是C语言没有预定义的函数指针,使用void*
指针调用函数在C语言中是非法的。有趣!typedef声明是以typedef作为存储类的声明
int ret = (*hw_func)();
稍后,您可以定义如下函数:
typedef int fc_name1 (void);
// this defines a function type called fc_name1
// which takes no parameter and returns int
您应该能够从c/c++标准中找到这一点 我以前从未见过对typedef名称执行此操作,但是函数名称周围的括号很有用,可以防止函数被扩展为类似函数的同名宏。例如,ctype.h
中的isxxx
函数被定义为函数和宏。这样,您就可以将指针指向isalpha
。但是C库如何定义越界的isalpha?可能是这样的:
fc_name1 myFunc;
// this is equivalent to the next line
// int myFunc(void);
#包括
int
(伊萨尔法)(国际c)
{
返回isalpha(c);
}
函数体中使用的isalpha
扩展为宏,函数头中使用的不是宏。有些人更喜欢将typedef定义为函数签名前置指针。这改变了将指针指向每个函数指针声明的“明确性”。我认为这是风格的问题。你也可以使用函数typedef
来声明函数(原型),但不能定义函数。但这不是很有用,除了模糊处理。@schot:Neat,我不知道它可以用于函数声明。实际上,我认为这对于声明一组回调函数是很有用的;以前我用一个预处理器宏来简化它。+1:Ah-这就是它变得有用的原因!我已经证明了你是正确的——但只有一次你告诉我怎么做。我们生活,我们学习;我想你仍然需要为每个函数定义包含整个函数原型?编辑:不必担心这个问题。请注意,您可以使用:int handler_func2(fc_name fptr){return fptr();}
,因为“function”参数会自动视为(转换为)函数指针。
typedef int (*myfunc)(void);
int helloword(void) {
printf("hello, world\n");
}
myfunc hw_func;
hw_func = helloworld;
int ret = (*hw_func)();
typedef int fc_name1 (void);
// this defines a function type called fc_name1
// which takes no parameter and returns int
fc_name1 myFunc;
// this is equivalent to the next line
// int myFunc(void);
#include <ctype.h>
int
(isalpha)(int c)
{
return isalpha(c);
}