void *function()是函数的指针还是返回void *的函数?
我对 void *function()
的含义感到困惑。
是函数的指针还是函数返回 void*
?我一直在数据结构上使用它作为递归函数返回指针,但是当我在多线程中看到代码( pthread
)时,有相同的函数声明。现在我很困惑它们之间有什么区别。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该功能具有返回类型
void *
。因此,在这种情况下,我总是更喜欢将符号
*
与函数名称分开,例如
jarod42
在评论中指向您可以使用the Trafing在C ++中重写C ++的函数声明返回类型,例如要声明指针的函数,则应
在返回类型为
void
或返回类型
void *
的位置写入。或指向函数的指针,将指针返回功能
The function has the return type
void *
.So I always prefer in such cases to separate the symbol
*
from the function name likeAnd as
Jarod42
pointed to in a comment you can rewrite the function declaration in C++ using the trailing return type likeIf you want to declare a pointer to function then you should write
where the return type is
void
Orwhere the return type
void *
.Or a pointer to function that returns pointer to function
每当我不确定C语法问题时,我都喜欢使用 cdecl href =“ https://cdecl.org” rel =“ noreferrer”>在线版本)要为我解释。它翻译在C语法和英语之间。
例如,我输入您的示例
void *foo()
,然后返回以查看其他语法的外观,我输入
将foo声明为函数返回void
的指针,然后返回当单个表达式中具有多个级别的类型,恒星或括号时,这将变得特别有用。
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 returnedTo see what the other syntax would look like, I input
declare foo as pointer to function returning void
and it returnedThis gets particularly useful when you have multiple levels of typecasts, stars, or brackets in a single expression.
它是将指针返回到
void
的功能。以这种方式考虑您的声明:
这将是返回
void
(或什么)的函数:以上方式考虑上述声明:
编写这些的一种更简单的方法是使用
typedef S:
这通常消除了围绕功能指针的混淆,并且更容易阅读。
It is a function returning a pointer to
void
.Think of your declaration this way:
This would be a function returning
void
(or nothing):Think of the above declaration this way:
A much easier way to write these is to use
typedef
s:This generally eliminates the confusion around function pointers and is much easier to read.
C/C ++中的声明是从标识符向外读取的, 。
快速查看()的优先级高于间接操作员
*
。因此,您的函数声明是这样读取的:从标识符开始:
function
isfunction()
一个不采用参数的函数void* function* function()
并返回avoid*
。该一般原则还具有数组声明(
[]
的优先级,其优先级高于*
)和两者的组合。因此,读取为
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
isfunction()
a function that takes no argumentsvoid* function()
and returns avoid*
.This general principle also holds with array declarations (
[]
also has higher precedence than*
) and combinations of the two. Sois read as
arr
isarr[42]
an array of 42 elements which are*arr[42]
pointers to(*arr[42])()
functions that take no arguments andint *(*arr[42])()
return anint*
.It takes a bit to get used to this, but once you've understood the principle, it's easy to read those declarations unambiguously.