你怎么看C声明? [英] How do you read C declarations?

查看:185
本文介绍了你怎么看C声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到的一些方法,但他们都没有坚持。我个人尽量避免复杂的类​​型在C和尝试将它们分解成组件的typedef。

我现在面临着来自所谓的三星级程序员保持一些遗留code和我有一个很难看的一些*** code [] []的。

你如何阅读复杂的C声明?


解决方案

本文介绍一个比较简单的7规则,这将让你阅读任何C声明,如果你发现自己想要或者需要手动操作:的 http://www.ericgiguere.com/articles/reading-c-declarations.html


  

      
  1. 找到的标识符。这是你的起点。在一张纸,写上声明标识符。

  2.   
  3. 向右边看。如果什么都没有,或者有一个右括号),转到第4步。

  4.   
  5. 您现在的定位不是一个数组(左括号)或函数(左括号)描述上。有可能是这些序列,或者结束一个无与伦比的右括号或声明符(分号或=初始化)结束。对于每一个这样的描述,从阅读左至右:


      
      

        
    • 如果空数组[],写数组

    •   
    • 如果有大小的数组,写上的数组大小

    •   
    • 如果一个函数(),写函数返回

    •   

      
      

    停在不完整的括号或声明符,以先到者为准的结束。


  6.   
  7. 返回到起始位置,看向左边。如果什么都没有,或者有一个左括号(,转到第6步。

  8.   
  9. 您现在的定位上的指针描述,*。可能存在的这些向左的序列,结束任一个无与伦比左括号(或声明符的开始,读由右至左,对于每个指针描述写指针。停在不匹配的括号或该声明符,以先到为准的开始。

  10.   
  11. 在这一点上,你要么一个括号前pression或完整的声明符。如果你有一个括号前pression,把它作为新的起点,回到步骤2。

  12.   
  13. 写下类型说明符。停止。

  14.   

如果你很好用的工具,那么我第二个建议使用程序 CDECL http://gd.tuwien.ac.at/linuxcommand.org/man_pages/cdecl1.html

I have heard of some methods, but none of them have stuck. Personally I try to avoid complex types in C and try to break them into component typedef.

I'm now faced with maintaining some legacy code from a so called 'three star programmer', and I'm having a hard time reading some of the ***code[][].

How do you read complex C declarations?

解决方案

This article explains a relatively simple 7 rules which will let you read any C declaration, if you find yourself wanting or needing to do so manually: http://www.ericgiguere.com/articles/reading-c-declarations.html

  1. Find the identifier. This is your starting point. On a piece of paper, write "declare identifier as".
  2. Look to the right. If there is nothing there, or there is a right parenthesis ")", goto step 4.
  3. You are now positioned either on an array (left bracket) or function (left parenthesis) descriptor. There may be a sequence of these, ending either with an unmatched right parenthesis or the end of the declarator (a semicolon or a "=" for initialization). For each such descriptor, reading from left to right:

    • if an empty array "[]", write "array of"
    • if an array with a size, write "array size of"
    • if a function "()", write "function returning"

    Stop at the unmatched parenthesis or the end of the declarator, whichever comes first.

  4. Return to the starting position and look to the left. If there is nothing there, or there is a left parenthesis "(", goto step 6.
  5. You are now positioned on a pointer descriptor, "*". There may be a sequence of these to the left, ending either with an unmatched left parenthesis "(" or the start of the declarator. Reading from right to left, for each pointer descriptor write "pointer to". Stop at the unmatched parenthesis or the start of the declarator, whichever is first.
  6. At this point you have either a parenthesized expression or the complete declarator. If you have a parenthesized expression, consider it as your new starting point and return to step 2.
  7. Write down the type specifier. Stop.

If you're fine with a tool, then I second the suggestion to use the program cdecl: http://gd.tuwien.ac.at/linuxcommand.org/man_pages/cdecl1.html

这篇关于你怎么看C声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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