无效(*(* F [])())() [英] void ( *( *f[] ) () ) ()

查看:124
本文介绍了无效(*(* F [])())()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到一个图今天想我AP preciate解释。因此,这里的图片:

我发现这个令人困惑,不知道如果这样的codeS永远是实用。我用Google搜索图片,发现另一张照片中这个 reddit的入口,这里是图片

所以这个读书螺旋的东西是否有效?请问这是怎么的C编译器解析?结果
如果有这种怪异的code简单解释这将会是巨大的。结果
除了所有,可这几样codeS是有用的?如果是这样,何时何地?

有<一个href=\"http://stackoverflow.com/questions/3707096/spiral-rule-and-declaration-follows-usage-for-parsing-c-and-c-declarations\">a关于螺旋规则的问题,但我不只是询问p $ pssions是它如何应用或如何前$该规则阅读。我质疑这些非pressions和螺旋规则的有效性的使用为好。对于这些,一些很好的答案已经发布。


解决方案

有一个规则叫顺时针/螺旋规则以帮助找到一个复杂的声明的含义。

C-FAQ :


  

有三个简单的步骤如下:


  
  

      
  1. 与未知的元素开始,移动螺旋/顺时针方向; ecountering以下元素在与相应的英文语句替换它们:


      
      

    [X] [] 结果
      =>的...阵列X规格或...数组未定义尺寸


      
      

    (TYPE1,TYPE2)结果
      =>传递函数Type1和Type2返回...


      
      

    * 结果
      =>指针(县)...


  2.   
  3. 请直到所有令牌已经涵盖在一个螺旋/顺时针方向这样做。


  4.   
  5. 始终解析括号中第一个东西!


  6.   

您可以检查上面的链接的例子。

另外请注意,帮你也有一个网站名为:

http://www.cdecl.org

您可以输入C声明,它会给其英文含义。对于

 无效(*(* F [])())()

它输出:


  

声明f,按指针数组函数返回函数指针返回void


编辑:

正如 Random832 在评论中指出,螺旋规则不涉及数组的数组,并会导致错误的结果中(大部分)的声明。例如,对于 INT ** X [1] [2]; 螺旋规则忽略了一个事实 [] 有更高的precedence超过 *

当在一组数组前,可以先采用螺旋规则之前添加这些括号。例如: INT ** X [1] [2]; 相同 INT **(X [1] [2]); (也适用C)由于precedence和螺旋规则,然后正确地把它读成x是指针数组2的阵列1的指针为int,这是正确的英文声明。

请注意,这个问题也已在本公约所涵盖<一个href=\"http://stackoverflow.com/questions/16260417/the-spiral-rule-about-declarations-when-is-it-in-error/16265389#16265389\">answer由詹姆斯·甘孜(通过的 haccks在评论)。

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

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:

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.

From c-faq:

There are three simple steps to follow:

  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] or []
    => Array X size of... or Array undefined size of...

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

    *
    => pointer(s) to...

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

  3. 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

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

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

it outputs:

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

EDIT:

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 *.

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.

Note that this issue has also been covered in this answer by James Kanze (pointed out by haccks in the comments).

这篇关于无效(*(* F [])())()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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