您如何阅读 C 声明? [英] How do you read C declarations?

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

问题描述

我听说过一些方法,但没有一个被卡住.我个人尽量避免在 C 中使用复杂类型,并尝试将它们分解为组件 typedef.

我现在面临着维护所谓三星级程序员"的一些遗留代码的问题,而且我很难阅读一些 ***code[][].

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

解决方案

本文解释了一个相对简单的 7 条规则,如果您发现自己想要或需要手动阅读任何 C 声明,它们将让您阅读:http://www.ericgiguere.com/articles/reading-c-declarations.html

<块引用>

  1. 找到标识符.这是你的起点.在一张纸上写下声明标识符为".
  2. 向右看.如果什么都没有,或者有一个右括号)",请转到第 4 步.
  3. 您现在位于数组(左括号)或函数(左括号)描述符上.可能有一系列这些,以不匹配的右括号或声明符的结尾(分号或="表示初始化)结束.对于每个这样的描述符,从左到右阅读:

    • 如果是空数组[]",则写数组的"
    • 如果数组有大小,写数组大小"
    • 如果是函数()",则写函数返回"

    停在不匹配的括号或声明符的末尾,以先到者为准.

  4. 回到起始位置,向左看.如果那里什么都没有,或者有一个左括号(",请转到第 6 步.
  5. 您现在位于指针描述符*"上.左边可能有这些序列,以不匹配的左括号("或声明符的开头结尾.从右到左读取,为每个指针描述符写入指向".在不匹配的括号处停止或声明符的开头,以先到者为准.
  6. 此时您有一个带括号的表达式或完整的声明符.如果您有一个带括号的表达式,请将其视为新的起点并返回到第 2 步.
  7. 写下类型说明符.停止.

如果你对某个工具没问题,那么我建议使用程序 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天全站免登陆