C 中带有函数指针的 Typedef?

发布于 2024-12-27 12:06:59 字数 217 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似梦非梦 2025-01-03 12:06:59

这是一种很难习惯的语法。

这个新类型的名称是什么?

类型是 getnxtbyte_t。 (您可以将尾随的 _t 读作“类型”。这是一种流行的约定。)

getnxtbyte_t 类型的变量可以保存采用一个 的函数的地址。 void * 参数并具有返回类型 void

它指向什么函数?

错误的问题。

该代码仅定义类型。没有创建变量,因此没有“it”来指向任何东西。

如果您知道具有正确签名的函数,例如:

void some_func(void*) {}

您现在可以使用该 typedef 创建指向它的指针:

getnxtbyte_t my_function_pointer = some_func;

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 one void * parameter and has return type 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:

getnxtbyte_t my_function_pointer = some_func;
甜尕妞 2025-01-03 12:06:59

typedef 创建一个名为 getnxtbyte_t 的类型。该类型用于指向返回void(即无任何内容)的函数的指针,如第二个单词所示。该函数采用一个参数,即 void *,如 stream 所示。

因此,如果您有一个具有如下声明的函数:

void some_function(void *any_name);

那么您可以使用像您帖子中那样的 typedef

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

This typedef creates a type called getnxtbyte_t. That type is for a pointer to a function that returns void (i.e. nothing), as shown in the second word. That function takes a single parameter, which is a void *, shown by 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:

void *some_param = NULL;
typedef void (*getnxtbyte_t)(void *stream); // declare typedef
getnxtbyte_t func = some_function; // assign
func(some_param); // call
讽刺将军 2025-01-03 12:06:59

函数指针类型名称为getnxtbyte_t。它现在不指向任何东西——这是指针的类型,而不是实际的指针。这就像说

typedef struct foo {int x;} Foo;

您定义了一个类型 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 saying

typedef struct foo {int x;} Foo;

you define a type Foo, but no actual instance of that type. And finally, yes, the function takes a single void* argument, and returns void.

新一帅帅 2025-01-03 12:06:59

我也是C语言新手,如有错误请指正。


指向函数的指针的格式如下:

datatype (*POINTER_NAME)(PARAMETERS);

这就是指向函数返回的数据类型指针的名称参数 > 指向的函数需要。

下面是函数指针与普通函数声明的比较:

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

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

typedef 允许我们创建自己的类型。

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

此时我们只声明了一个类型;我们没有指出任何事情。因此,让我们创建一个名为 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);

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:

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.

// 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 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文