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] 完全不同。
谢谢 Raja
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] 完全不同。
谢谢 Raja
它们是完全不同的。第一个是数组,第二个是指向数组的指针。
你在第一个 bar
声明后的注释绝对是错误的。第一个 bar
是一个由10个 int
组成的数组。这就是它的全部。它不是一个指针(也就是说,你所说的 "which means" 部分没有任何意义)。
可以这样表达:
typedef int T[10];
你的第一个bar
的类型是T
,而你的第二个bar
的类型是T *
。你理解T
和T *
之间的区别了吗?
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
这两个声明并不声明相同的类型。
第一个声明声明了一个int数组。
第二个声明声明了一个指向int数组的指针。
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.
还有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