在 C/C++ 中如何声明一个指向函数的指针,该函数返回一个指向 int 值数组的指针?
这是正确的吗?
int (*(*ptr)())[];
我知道这是微不足道的,但我正在查看有关此类构造的旧测试,而这种特定的组合并未出现在测试中,这真的让我发疯;我只需要确定一下。对于此类声明是否有一个明确且可靠的可理解的规则? (即:指向...数组的指针...指向...函数的指针...等等) 谢谢!
右
Is this correct?
int (*(*ptr)())[];
I know this is trivial, but I was looking at an old test about these kind of constructs, and this particular combination wasn't on the test and it's really driving me crazy; I just have to make sure. Is there a clear and solid understandable rule to these kind of declarations?
(ie: pointer to... array of.. pointers to... functions that.... etc etc)
Thanks!
R
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从右到左的规则让一切变得简单。
int (*(*ptr)())[];
可以解释为从变量名开始-------------------- ----------
ptr
右边除了
)
之外什么都没有,所以向左走可以找到*
-------------- 是一个指针跳出括号,遇到
()
-------------- 到一个不带参数的函数(如果是 C 未指定参数数量的情况)向左,找到
*
-------------- ---------------------------------- 并返回一个指针跳转括号,向右走,点击
[]
---------- 到一个数组再向左走,找到
int
--- ----------------------------------整数
。The right-left rule makes it easy.
int (*(*ptr)())[];
can be interpreted asStart from the variable name -------------------------------
ptr
Nothing to right but
)
so go left to find*
-------------- is a pointerJump out of parentheses and encounter
()
----------- to a function that takes no arguments(in case of C unspecified number of arguments)Go left, find
*
------------------------------------------------ and returns a pointerJump put of parentheses, go right and hit
[]
---------- to an array ofGo left again, find
int
-------------------------------------ints
.几乎在所有想要返回指向数组的指针的情况下,最简单的做法就是返回指向数组第一个元素的指针。该指针可以在与数组名称相同的上下文中使用,并且与返回“指向数组的指针”类型的指针相比,它不提供更多或更少的间接性,实际上它将保存相同的指针值。
如果您遵循此操作,您需要一个指向函数的指针,该函数返回一个指向
int
的指针。您可以构建它(声明的构建比解析更容易)。指向
int
的指针:返回指向
int
的指针的函数:返回指向
int
的指针的函数的指针:如果您确实想返回指向 a 的指针函数返回一个指向
int
数组的指针,您可以遵循相同的过程。int 数组:
指向 int 数组的指针:
返回指针的函数 ... :
指向 fn ... 的指针:
这就是您所拥有的。
In almost all situations where you want to return a pointer to an array the simplest thing to do is to return a pointer to the first element of the array. This pointer can be used in the same contexts as an array name an provides no more or less indirection than returning a pointer of type "pointer to array", indeed it will hold the same pointer value.
If you follow this you want a pointer to a function returning a pointer to an
int
. You can build this up (construction of declarations is easier than parsing).Pointer to
int
:Function returning pointer to
int
:pointer to function returning a pointer to
int
:If you really want to return a pointer to a function returning a pointer to an array of
int
you can follow the same process.Array of int:
Pointer to array of int:
Function returning pointer ... :
Pointer to fn ... :
which is what you have.
你不知道。只需将其分成两种 typedef:一种用于指向 int 数组的指针,另一种用于指向函数的指针。例如:
这不仅更具可读性,还可以更轻松地声明/定义要分配给变量的函数:
当然,这假设这是现实世界的情况,而不是面试问题或家庭作业。我仍然认为对于这些情况来说这是一个更好的解决方案,但这只是我的观点。 :)
You don't. Just split it up into two typedefs: one for pointer to int array, and one for pointer to functions. Something like:
This is not only more readable, it also makes it easier to declare/define the functions that you are going to assign the variable:
Of course this assumes this was a real-world situation, and not an interview question or homework. I would still argue this is a better solution for those cases, but that's only me. :)
如果您想作弊:
在 gcc 上编译它会产生:
从“int (*(*)())[5]”到“int”的无效转换
。第一个位是您要寻找的类型。当然,一旦您有了
PtrToArray
typedef,整个练习就变得相当简单,但有时如果您已经有了函数名称并且只需将其粘贴到某个地方,这会派上用场。而且,无论出于何种原因,您都不能依靠模板欺骗来向您隐藏血淋淋的细节。如果你的编译器支持它,你也可以这样做:
在我的电脑上,它会生成
void function(T) [with T = int (* (*)())[5]]
能够读取类型非常有用,因为理解编译器错误通常取决于您弄清楚所有这些括号含义的能力。但在我看来,自己制作它们的用处不大。
If you feel like cheating:
Compiling that on gcc yields:
invalid conversion from 'int (*(*)())[5]' to 'int'
. The first bit is the type you're looking for.Of course, once you have your
PtrToArray
typedef, the whole exercise becomes rather more trivial, but sometimes this comes in handy if you already have the function name and you just need to stick it somewhere. And, for whatever reason, you can't rely on template trickery to hide the gory details from you.If your compiler supports it, you can also do this:
Which, on my computer box, produces
void function(T) [with T = int (* (*)())[5]]
Being able to read the types is pretty useful, since understanding compiler errors is often dependent on your ability to figure out what all those parenthesis mean. But making them yourself is less useful, IMO.
这是我的解决方案...
函子返回 int* 数组。它并不像您的解决方案那么复杂。
Here's my solution...
Functor returning an array of int*'s. It isn't as complicated as your solution.
获得以下
使用 cdecl,您可以从 C-faq 此问题 类似,但提供了 3 种方法来解决问题。
Using cdecl you get the following
This question from C-faq is similar but provides 3 approaches to solve the problem.