简体   繁体   English

Typedef与C中的函数指针?

[英]Typedef with function pointers in C?

I'm pretty new to C, and I'm having a really hard time reading this line of code and understanding it: 我对C还是很陌生,在阅读和理解这一行代码时我真的很难受:

typedef void (*getnxtbyte_t)(void *stream);

From looking around, I now know that it is for a pointer pointing to a function. 通过环顾四周,我现在知道它是指向函数的指针。 But could anyone help me clarify this even further? 但是有人可以帮我进一步澄清这一点吗? What is the name of this new type? 这种新类型的名称是什么? What function is it pointing to? 它指向什么功能? Is the parameter of the function (void* stream) ? 函数的参数(void* stream)吗?

Thanks in advance! 提前致谢!

It is a tricky syntax to get used to. 这是一个难以掌握的语法。

What is the name of this new type? 这种新类型的名称是什么?

The type is getnxtbyte_t . 类型是getnxtbyte_t (You can read that trailing _t as "type". It's a popular convention.) (您可以将尾随_t读为“类型”。这是一种流行的约定。)

A variable of type getnxtbyte_t can hold the address of a function that takes one void * parameter and has return type void . getnxtbyte_t类型的变量可以保存带有一个void *参数且返回类型为void的函数的地址。

What function is it pointing to? 它指向什么功能?

Wrong question. 错误的问题。

That code merely defines the type. 该代码仅定义类型。 No variables are created so there's no "it" to point to anything. 没有创建任何变量,因此没有“它”指向任何东西。

If you know of a function with the correct signature, such as: 如果您知道具有正确签名的函数,例如:

void some_func(void*) {}

You may now create a pointer to it using that typedef: 您现在可以使用该typedef创建指向它的指针:

getnxtbyte_t my_function_pointer = some_func;

This typedef creates a type called getnxtbyte_t . typedef创建一个名为getnxtbyte_t的类型。 That type is for a pointer to a function that returns void (ie nothing), as shown in the second word. 该类型用于指向返回void (即无结果)的函数的指针,如第二个单词所示。 That function takes a single parameter, which is a void * , shown by stream . 该函数采用单个参数,即void * ,由stream

So if you had a function with a declaration like this: 因此,如果您的函数带有这样的声明:

void some_function(void *any_name);

Then you could use a typedef like the one in your post: 然后,您可以像在帖子中那样使用typedef

void *some_param = NULL;
typedef void (*getnxtbyte_t)(void *stream); // declare typedef
getnxtbyte_t func = some_function; // assign
func(some_param); // call

The function pointer type name is getnxtbyte_t . 函数指针类型的名称为getnxtbyte_t It's not pointing to anything now -- this is a type of pointer, not an actual pointer. 它不指向任何东西了-这是一个类型的指针,而不是实际的指针。 It's just like saying 就像在说

typedef struct foo {int x;} Foo;

you define a type Foo , but no actual instance of that type. 您定义类型Foo ,但没有该类型的实际实例。 And finally, yes, the function takes a single void* argument, and returns void . 最后,是的,该函数接受单个void*参数,并返回void

I am also new to C, so if there are any errors please correct me. 我也是C语言的新手,所以如果有任何错误,请纠正我。


A pointer that points to a function is formatted like so: 指向函数的指针的格式如下:

datatype (*POINTER_NAME)(PARAMETERS);

So that's the data type the pointed function returns, the name of the pointer and the parameters the pointed function takes. 这就是指针函数返回的数据类型指针名称以及指针函数需要的参数


Here's how a function pointer looks compared to a normal function declaration: 与普通函数声明相比,函数指针的外观如下:

// normal function declaration
void getnxtbyte_t(void *stream);

// function pointer
void (*getnxtbyte_t)(void *stream);


typedef allows us to create our own type. typedef允许我们创建自己的类型。

// will create a type called getnxtbyte_t
typedef void (*getnxtbyte_t)(void *stream);


At this point we have only declared a type; 至此,我们只声明了一个类型。 we are not pointing to anything. 我们没有指向任何东西。 So let's create a pointer named func_ptr and point it to a function. 因此,让我们创建一个名为func_ptr的指针并将其指向一个函数。

// func_ptr is a pointer of type getnxtbyte_t
getnxtbyte_t func_ptr = another_function;

// calling func_ptr is now the same as calling another_function
func_ptr(an_argument);

// had we not used typedef, we would type:
void (*getnxtbyte_t)(void *stream) = another_func;
getnxtbyte_t(an_argument);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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