c/c++语言开发共享等价C声明

int (*x)[10]; 

 int x[10]; 

当量?

根据“顺时针螺旋”规则,它们解析为不同的C声明。

对于点击厌倦:

大卫安德森的“顺时针/螺旋规则”

有一种称为“顺时针/螺旋规则”的技术,可以让任何C程序员在头脑中解析任何C声明!

有三个简单的步骤:

  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! 

    他们不平等。 在第一种情况下, x是指向10个整数数组的指针,在第二种情况下, x是10个整数的数组。

    这两种类型是不同的。 通过在两种情况下检查sizeof ,你可以看到它们不是一回事。

    阅读声明时,请遵循以下简单过程 :

    从变量名称开始(如果没有标识符,则从最里面的构造开始。向右看,不要跳过右括号;说出你看到的内容。再看左边而不跳过括号;说出你看到的内容。如果你看到了,请跳出一个括号任何。看起来正确;说出你看到的东西。向左看;说出你看到的东西。继续这样,直到你说变量类型或返回类型。

    所以:

     int (*x)[10]; 

    x是指向10个int的数组的指针

     int x[10]; 

    x是10个int的数组

     int *x[10]; 

    x是一个包含10个指向int的指针的数组

    我倾向于遵循理解C规则的优先规则,这是在Peter van der Linden的专家C编程 – 深C秘密一书中非常好地给出的。

     A - Declarations are read by starting with the name and then reading in precedence order. B - The precedence, from high to low, is: B.1 parentheses grouping together parts of a declaration B.2 the postfix operators: parentheses () indicating a function, and square brackets [] indicating an array. B.3 the prefix operator: the asterisk denoting "pointer to". C If a const and/or volatile keyword is next to a type specifier (eg int, long, etc.) it applies to the type specifier. Otherwise the const and/or volatile keyword applies to the pointer asterisk on its immediate left. 

    对我来说,更容易记住规则,因为在*之前没有任何明确的分组, ()[]绑定。 因此,对于像这样的声明

     T *a[N]; 

    []绑定在*之前,所以a是指针的N元素数组。 按步骤分解:

      a -- a a[N] -- is an N-element array *a[N] -- of pointer T *a[N] -- to T. 

    对于像这样的声明

     T (*a)[N]; 

    parens强迫*[]之前绑定,所以

      a -- a (*a) -- is a pointer (*a)[N] -- to an N-element array T (*a)[N] -- of T 

    它仍然是顺时针/螺旋规则,只是以更紧凑的方式表达。

    不。第一个声明一个包含10个int指针的数组,第二个声明一个10个int的数组。

    需要了解更多c/c++开发分享等价C声明,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

      以上就是c/c++开发分享等价C声明相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/979756.html

      (0)
      上一篇 2021年12月13日
      下一篇 2021年12月13日

      精彩推荐