void *function()是函数的指针还是返回void *的函数?

发布于 2025-01-22 12:31:55 字数 171 浏览 1 评论 0 原文

我对 void *function()的含义感到困惑。
是函数的指针还是函数返回 void*?我一直在数据结构上使用它作为递归函数返回指针,但是当我在多线程中看到代码( pthread )时,有相同的函数声明。现在我很困惑它们之间有什么区别。

I'm confused about the meaning of void *function().
Is it a pointer to function or a function returning void*? I've always used it on data structures as a recursive function returning a pointer, but when i saw a code in multithreading (pthread) there is a same function declaration. Now I'm confused what's the difference between them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

你另情深 2025-01-29 12:31:55

该功能具有返回类型 void *

void *function();

因此,在这种情况下,我总是更喜欢将符号*与函数名称分开

void * function();

,例如 jarod42 在评论中指向您可以使用the Trafing在C ++中重写C ++的函数声明返回类型,

auto function() -> void *;

例如要声明指针的函数,则应

void ( *function )();

在返回类型为 void

void * ( *function )();

返回类型 void *的位置写入。

或指向函数的指针,将指针返回功能

void * ( *( *function )() )();

The function has the return type void *.

void *function();

So I always prefer in such cases to separate the symbol * from the function name like

void * function();

And as Jarod42 pointed to in a comment you can rewrite the function declaration in C++ using the trailing return type like

auto function() -> void *;

If you want to declare a pointer to function then you should write

void ( *function )();

where the return type is void Or

void * ( *function )();

where the return type void *.

Or a pointer to function that returns pointer to function

void * ( *( *function )() )();
森林迷了鹿 2025-01-29 12:31:55

每当我不确定C语法问题时,我都喜欢使用 cdecl href =“ https://cdecl.org” rel =“ noreferrer”>在线版本)要为我解释。它翻译在C语法和英语之间。

例如,我输入您的示例 void *foo(),然后返回

将foo声明为函数,将指针返回void

以查看其他语法的外观,我输入将foo声明为函数返回void 的指针,然后返回

void(*foo)()

当单个表达式中具有多个级别的类型,恒星或括号时,这将变得特别有用。

Whenever I'm unsure about C syntax issues, I like to use the cdecl utility (online version) to interpret for me. It translates between C syntax and English.

For example, I input your example of void *foo() and it returned

declare foo as function returning pointer to void

To see what the other syntax would look like, I input declare foo as pointer to function returning void and it returned

void (*foo)()

This gets particularly useful when you have multiple levels of typecasts, stars, or brackets in a single expression.

余厌 2025-01-29 12:31:55

它是将指针返回到 void 的功能。

以这种方式考虑您的声明:

void *(function());

这将是返回 void (或什么)的函数:

void (*function2)();

以上方式考虑上述声明:

void ((*function2)());

编写这些的一种更简单的方法是使用 typedef S:

typedef void *function_returning_void_pointer();
typedef void function_returning_nothing();

function_returning_void_pointer function;
function_returning_nothing *function2;

这通常消除了围绕功能指针的混淆,并且更容易阅读。

It is a function returning a pointer to void.

Think of your declaration this way:

void *(function());

This would be a function returning void (or nothing):

void (*function2)();

Think of the above declaration this way:

void ((*function2)());

A much easier way to write these is to use typedefs:

typedef void *function_returning_void_pointer();
typedef void function_returning_nothing();

function_returning_void_pointer function;
function_returning_nothing *function2;

This generally eliminates the confusion around function pointers and is much easier to read.

当爱已成负担 2025-01-29 12:31:55

C/C ++中的声明是从标识符向外读取的, 。

快速查看()的优先级高于间接操作员*。因此,您的函数声明是这样读取的:

  • 从标识符开始: function is

  • function()一个不采用参数的函数

  • void* function* function()并返回a void*

该一般原则还具有数组声明( [] 的优先级,其优先级高于*)和两者的组合。因此,

int *(*arr[42])();

读取为

  • arr
  • arr [42] 42个元素的数组是
  • *arr [42] 指针到
  • (*) ARR [42])()函数不采用任何参数,
  • int*(*arr [42])()返回 int*

要习惯这一点需要一点时间,但是一旦您理解了原则,就很容易明确阅读这些声明。

Declarations in C/C++ are read from the identifier outwards following operator precedence.

A quick look at the C/C++ operator precedence table in wikipedia reveals that the function call operator () has a higher precedence than the indirection operator *. So, your function declarations reads like this:

  • Start at the identifier: function is

  • function() a function that takes no arguments

  • void* function() and returns a void*.

This general principle also holds with array declarations ([] also has higher precedence than *) and combinations of the two. So

int *(*arr[42])();

is read as

  • arr is
  • arr[42] an array of 42 elements which are
  • *arr[42] pointers to
  • (*arr[42])() functions that take no arguments and
  • int *(*arr[42])() return an int*.

It takes a bit to get used to this, but once you've understood the principle, it's easy to read those declarations unambiguously.

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