C并不那么难:void(*(* f [])())() [英] C isn't that hard: void ( *( *f[] ) () ) ()

查看:108
本文介绍了C并不那么难:void(*(* f [])())()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天刚刚看过一张照片,并认为我会很高兴地进行解释.所以这是图片:

I just saw a picture today and think I'd appreciate explanations. So here is the picture:

我发现这令人困惑,并且想知道这样的代码是否可行.我在图片上用Google搜索,并在 reddit条目中找到了另一张图片,这是这张照片:

I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture:

因此,这种螺旋式阅读"是有效的吗?这是C编译器解析的方式吗?
如果对此怪异的代码有更简单的解释,那就太好了.
除此以外,这类代码是否有用?如果是这样,何时何地?

So this "reading spirally" is something valid? Is this how C compilers parse?
It'd be great if there are simpler explanations for this weird code.
Apart from all, can these kind of codes be useful? If so, where and when?

存在问题关于螺旋规则",但我不仅在询问如何应用该规则或如何使用该规则读取表达式.我也质疑这种表达方式的使用以及螺旋规则的有效性.关于这些,已经发布了一些不错的答案.

There is a question about "spiral rule", but I'm not just asking about how it's applied or how expressions are read with that rule. I'm questioning usage of such expressions and spiral rule's validity as well. Regarding these, some nice answers are already posted.

推荐答案

有一个名为,以帮助查找复杂声明的含义.

There is a rule called the "Clockwise/Spiral Rule" to help find the meaning of a complex declaration.

来自 c-faq :

要遵循三个简单步骤:

There are three simple steps to follow:

  1. 从未知元素开始,沿螺旋/顺时针方向移动;在遇到以下元素时,请用相应的英语声明替换它们:

  1. Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements:

[X][]
=>数组X大小为...或数组未定义大小为...

[X] or []
=> Array X size of... or Array undefined size of...

(type1, type2)
=>函数传递type1和type2返回...

(type1, type2)
=> function passing type1 and type2 returning...

*
=>指向...的指针

*
=> pointer(s) to...

沿螺旋/顺时针方向执行此操作,直到所有标记都被覆盖.

Keep doing this in a spiral/clockwise direction until all tokens have been covered.

总是先解决括号中的任何内容!

Always resolve anything in parenthesis first!

您可以在上面的链接中查看示例.

You can check the link above for examples.

还请注意,为了帮助您,还有一个网站:

Also note that to help you there is also a website called:

http://www.cdecl.org

您可以输入C声明,它将给出其英语含义.对于

You can enter a C declaration and it will give its english meaning. For

void (*(*f[])())()

它输出:

将f声明为指向函数的指针数组,返回指向void的函数的指针

declare f as array of pointer to function returning pointer to function returning void

Random832 的评论所指出的那样,螺旋规则无法解决数组数组问题,并且会导致错误的结果导致了(大多数)这些声明.例如,对于int **x[1][2];,螺旋规则忽略了[]优先于*的事实.

As pointed out in the comments by Random832, the spiral rule does not address array of arrays and will lead to a wrong result in (most of) those declarations. For example for int **x[1][2]; the spiral rule ignores the fact that [] has higher precedence over *.

在数组数组前面时,可以先应用显式括号,然后再应用螺旋规则.例如:由于优先级,int **x[1][2];int **(x[1][2]);相同(也是有效的C),然后螺旋规则正确地读取它,因为"x是指向int的指针的数组2的数组1"是正确的英文声明.

When in front of array of arrays, one can first add explicit parentheses before applying the spiral rule. For example: int **x[1][2]; is the same as int **(x[1][2]); (also valid C) due to precedence and the spiral rule then correctly reads it as "x is an array 1 of array 2 of pointer to pointer to int" which is the correct english declaration.

请注意,此 James Kanze (由 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆