gpt4 book ai didi

c - 你如何阅读 C 声明?

转载 作者:太空狗 更新时间:2023-10-29 16:19:52 25 4
gpt4 key购买 nike

我听说过一些方法,但没有一个坚持下来。就个人而言,我尽量避免在 C 中使用复杂类型,并尝试将它们分解为组件 typedef。

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

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

最佳答案

本文介绍了一个相对简单的 7 条规则,如果您发现自己想要或需要手动阅读任何 C 声明,这些规则可以让您阅读: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.

如果您对某个工具没问题,那么我支持使用程序 cdecl 的建议:http://gd.tuwien.ac.at/linuxcommand.org/man_pages/cdecl1.html

关于c - 你如何阅读 C 声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/89056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com