简体   繁体   English

这些函数typedef是什么意思?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM