问题描述
我要声明指针类型,指向一个功能,所以我尝试:
无效的typedef(*打印)(无效);
完美的作品
无效(*打印)(无效);
p是ponter变量,而不是一个类型。
的typedef(无效)(*打印)(无效);
错误预期标识符或(前无效
无效的typedef(*)(无效)打印;
错误:预期'=',',',',','ASM'或'_的属性的_'前'打印'
我的问题是:
-
我必须使用
的typedef
来声明函数指针类型? -
为什么
的typedef(无效)(*打印)(无效);
是错的?什么()
意味着在这里? -
为什么我不能这样写的:无效的typedef(*)(无效)打印
正确的方法是:
无效的typedef(* print_function_ptr)(无效)
和其变量使用/参数声明是:
print_function_ptr磷;
-
您不需要一个typedef来声明一个变量。您可以直接写
无效(* P)(无效)
来声明一个变量P
指着利用函数无效
并返回无效
。然而,为一个函数指针声明一个类型别名/名称,的typedef
是工具。 -
这并不意味着什么,它不是一个有效的C语法。
-
由于它不是C如何工作的。用C模仿类型定义的变量是如何声明或定义。
I want to declare a pointer type which point to a function, so I try:
typedef void (*print)(void);
works perfect
void (*print)(void);
p is a ponter variable , not a type.
typedef (void) (*print)(void);
error expected identifier or ‘(’ before ‘void’
typedef void (*)(void) Print;
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘_ attribute _’ before ‘Print’ .
My question is:
Do I have to use
typedef
to declare a function pointer type ?Why
typedef (void) (*print)(void);
is wrong ? what()
means here?Why I can't write in this way:
typedef void (*)(void) Print
?
The correct way is:
typedef void (*print_function_ptr)(void)
and its usage for variable/parameter declaration is:
print_function_ptr p;
You don't need a typedef to declare a variable. You can directly write
void (*p)(void)
to declare a variablep
pointing to a function takingvoid
and returningvoid
. However to declare a type alias / name for a pointer to function,typedef
is the tool.It does not mean anything it is not a valid C syntax.
Because it is not how C works. Typedefs in C mimics how variables are declared or defined.
这篇关于的typedef函数指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!