【问题标题】:difference between int bar[10] vs int (*bar)[10]int bar[10] 与 int (*bar)[10] 之间的区别
【发布时间】:2011-11-24 00:05:32
【问题描述】:
int bar[10]; /* bar is array 10 of int, which means bar is a pointer to array 10 of int */

int (*bar)[10]; /* bar is a pointer to array 10 of int */

在我看来他们都是一样的,我错了吗?请告诉我。

编辑:int *bar[10] 完全不同。

谢谢 拉贾

【问题讨论】:

  • 数组不是指针指针不是数组。阅读comp.lang.c faq 的第 6 节。既然你已经在那里了,也请阅读其他部分:-)

标签: c arrays pointers types


【解决方案1】:

它们完全不同。第一个是数组。第二个是指向数组的指针。

您在第一个bar 声明之后的评论绝对不正确。第一个bar 是一个包含10 个ints 的数组。时期。它不是一个指针(即你的“这意味着”部分根本没有意义)。

可以这样表达:

typedef int T[10];

您的第一个bar 的类型为T,而您的第二个bar 的类型为T *。你明白TT * 的区别吗?

【讨论】:

  • 哦!所以在第二种情况下,当我做 bar++ 时,它会跳过所有 10 个并指向下一组数组?
  • +1。我认为 OP 很困惑,因为在 C 中的某些上下文中,数组会自动衰减为指向其第一个元素的指针。第一个 bar 可能是代码中某些地方的指针 - 而不是指向数组的指针就像你的第二个例子一样。
【解决方案2】:

你可以这样做:

int a[10];

int (*bar)[10] = &a;  // bar now holds the address of a

(*bar)[0] = 5;  // Set the first element of a

但你不能这样做:

int a[10];

int bar[10] = a;  // Compiler error!  Can't assign one array to another

【讨论】:

    【解决方案3】:

    这两个声明没有声明相同的类型。

    您的第一个声明声明了一个 int 数组。

    您的第二个声明声明了一个指向 int 数组的指针。

    【讨论】:

      【解决方案4】:

      这是一个关于 C“左右规则”的链接,我在阅读复杂的 c 声明时发现它很有用:http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html。它还可以帮助您了解int bar[10]int (*bar)[10] 之间的区别。

      摘自文章:

      First, symbols.  Read
      
           *      as "pointer to"         - always on the left side
           []     as "array of"           - always on the right side
           ()     as "function returning" - always on the right side
      
      as you encounter them in the declaration.
      
      STEP 1
      ------
      Find the identifier.  This is your starting point.  Then say to yourself,
      "identifier is."  You've started your declaration.
      
      STEP 2
      ------
      Look at the symbols on the right of the identifier.  If, say, you find "()"
      there, then you know that this is the declaration for a function.  So you
      would then have "identifier is function returning".  Or if you found a 
      "[]" there, you would say "identifier is array of".  Continue right until
      you run out of symbols *OR* hit a *right* parenthesis ")".  (If you hit a 
      left parenthesis, that's the beginning of a () symbol, even if there
      is stuff in between the parentheses.  More on that below.)
      
      STEP 3
      ------
      Look at the symbols to the left of the identifier.  If it is not one of our
      symbols above (say, something like "int"), just say it.  Otherwise, translate
      it into English using that table above.  Keep going left until you run out of
      symbols *OR* hit a *left* parenthesis "(".  
      
      Now repeat steps 2 and 3 until you've formed your declaration.
      

      【讨论】:

      【解决方案5】:

      还有cdecl(1)http://cdecl.org/

      $ cdecl explain 'int bar[10]'
      declare bar as array 10 of int
      
      $ cdecl explain 'int (*bar)[10]'
      declare bar as pointer to array 10 of int
      

      【讨论】:

        猜你喜欢
        • 2014-05-13
        • 2021-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        相关资源
        最近更新 更多