C 中带有函数指针的 Typedef?
我对 C 还很陌生,我很难阅读这行代码并理解它:
typedef void (*getnxtbyte_t)(void *stream);
通过环顾四周,我现在知道它是指向函数的指针。但有人可以帮我进一步澄清这一点吗?这种新类型的名称是什么?它指向什么函数?函数的参数是(void*stream)
吗?
提前致谢!
I'm pretty new to C, and I'm having a really hard time reading this line of code and understanding it:
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)
?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种很难习惯的语法。
这个新类型的名称是什么?
类型是
getnxtbyte_t
。 (您可以将尾随的_t
读作“类型”。这是一种流行的约定。)getnxtbyte_t
类型的变量可以保存采用一个的函数的地址。 void *
参数并具有返回类型void
。它指向什么函数?
错误的问题。
该代码仅定义类型。没有创建变量,因此没有“it”来指向任何东西。
如果您知道具有正确签名的函数,例如:
您现在可以使用该 typedef 创建指向它的指针:
It is a tricky syntax to get used to.
What is the name of this new type?
The type is
getnxtbyte_t
. (You can read that trailing_t
as "type". It's a popular convention.)A variable of type
getnxtbyte_t
can hold the address of a function that takes onevoid *
parameter and has return typevoid
.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:
You may now create a pointer to it using that typedef:
此
typedef
创建一个名为getnxtbyte_t
的类型。该类型用于指向返回void
(即无任何内容)的函数的指针,如第二个单词所示。该函数采用一个参数,即void *
,如stream
所示。因此,如果您有一个具有如下声明的函数:
那么您可以使用像您帖子中那样的
typedef
:This
typedef
creates a type calledgetnxtbyte_t
. That type is for a pointer to a function that returnsvoid
(i.e. nothing), as shown in the second word. That function takes a single parameter, which is avoid *
, shown bystream
.So if you had a function with a declaration like this:
Then you could use a
typedef
like the one in your post:函数指针类型名称为
getnxtbyte_t
。它现在不指向任何东西——这是指针的类型,而不是实际的指针。这就像说您定义了一个类型
Foo
,但没有该类型的实际实例。最后,是的,该函数采用单个void*
参数,并返回void
。The function pointer type name is
getnxtbyte_t
. It's not pointing to anything now -- this is a type of pointer, not an actual pointer. It's just like sayingyou define a type
Foo
, but no actual instance of that type. And finally, yes, the function takes a singlevoid*
argument, and returnsvoid
.我也是C语言新手,如有错误请指正。
指向函数的指针的格式如下:
这就是指向函数返回的数据类型、指针的名称和参数 > 指向的函数需要。
下面是函数指针与普通函数声明的比较:
typedef
允许我们创建自己的类型。此时我们只声明了一个类型;我们没有指出任何事情。因此,让我们创建一个名为 func_ptr 的指针并将其指向一个函数。
I am also new to C, so if there are any errors please correct me.
A pointer that points to a function is formatted like so:
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:
typedef
allows us to create our own type.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.