[英]What do these function typedefs mean?
I am trying to understand what the following typedefs mean. 我试图了解以下typedef的含义。 Are they function pointers?
它们是函数指针吗?
typedef int Myfunc(char *);
static Myfunc myfunc;
int myfunc(char *string)
{
printf("%s\n", string);
return 0;
}
I know typedef int Myfunc(char *)
means func Myfunc
return an integer,that's all,all right? 我知道
typedef int Myfunc(char *)
意味着func Myfunc
返回一个整数,就这样,好吗? And I thought, next statement, how could call myfunc
? 我想,下
myfunc
语句,怎么称呼myfunc
? It should be this way static Myfunc *myfunc
, mean a function pointer,isn't it? static Myfunc *myfunc
应该是这种方式,是一个函数指针,不是吗?
The second line is a declaration of a function, not a function pointer. 第二行是函数的声明,而不是函数指针。 The function is of type
MyFunc
, is called myfunc
, and has static linkage : meaning that the function is not available to other source files compiled into the same object. 该函数的类型为
MyFunc
,称为myfunc
,并且具有静态链接 :意味着该函数不适用于编译到同一对象中的其他源文件。
The signature for the myfunc
is: typedef int (*MyFunc)(char *);
myfunc
的签名为: typedef int (*MyFunc)(char *);
Then you can declare a variable of type MyFunc
ie, 然后,您可以声明
MyFunc
类型的变量,即
static MyFunc func_ptr;
You can then assign a function matching the signature to this variable. 然后,您可以将与签名匹配的函数分配给该变量。
调用myfunc
与函数调用相同:
myfunc("a-string");
I'm not sure that's valid code. 我不确定这是有效的代码。
typedef int (*Myfunc)(char *);
declares a type Myfunc
, that is a pointer to a function that takes a char *
and returns int
. 声明类型
Myfunc
,该类型是指向使用char *
并返回int
的函数的指针。
You cannot forward declare a function with a typedef. 您不能使用typedef转发声明函数。 Omit
static Myfunc myfunc;
省略
static Myfunc myfunc;
and instead begin your function definition with 而是从以下位置开始函数定义
static int myfunc(char *string) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.