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]完全不同。
由于 拉加
答案 0 :(得分:9)
他们完全不同。第一个是数组。第二个是指向数组的指针。
第一个bar
声明后的注释绝对不正确。第一个bar
是10个int
的数组。期。它不是指针(即你的“意思是”部分完全没有意义)。
可以这样表达:
typedef int T[10];
您的第一个bar
类型为T
,而您的第二个bar
类型为T *
。您了解T
和T *
之间的区别,是吗?
答案 1 :(得分:3)
你可以这样做:
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
答案 2 :(得分:1)
这两个声明不会声明相同的类型。
您的第一个声明声明了一个int数组。
你的第二个声明声明了一个指向int数组的指针。
答案 3 :(得分:0)
这是一个关于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.
答案 4 :(得分:0)
还有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