What is the difference between int *a[3]
and int (*a)[3]
?
int * a [3]和int(* a)[3]之间有什么区别?
5 个解决方案
#1
19
There is no difference between int a[3]
and int (a)[3]
, they both declare a
as an array of 3 int
s. There is a difference between int *a[3]
and int (*a)[3]
, the former declares an array of 3 pointers to int
whereas the second declares a pointer to an array of 3 int
s. The parenthesis make a difference here because in C brackets have a higher precedence than *.
int a [3]和int(a)[3]之间没有区别,它们都声明a为3个整数的数组。 int * a [3]和int(* a)[3]之间存在差异,前者声明了一个指向int的3个指针的数组,而第二个声明了指向3个int的数组的指针。括号在这里有所不同,因为在C括号中的优先级高于*。
#2
9
Alternatively, you can use cdecl
, which outputs the meaning of variable declarations in English.
或者,您可以使用cdecl,它以英语输出变量声明的含义。
cdecl> explain int*a[3]
declare a as array 3 of pointer to int
声明一个指向int的指针数组3
cdecl> explain int (*a) [3]
declare a as pointer to array 3 of int
声明一个指向int数组3的指针
#3
7
If you have any doubt, using this g++ trick is often handy:
如果您有任何疑问,使用这个g ++技巧通常很方便:
#include <iostream>
template < class T > void describe(T& )
{
// With msvc, use __FUNCSIG__ instead
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main(int argc, char* argv[])
{
int *a[3];
describe(a);
int (*b)[3];
describe(b);
return EXIT_SUCCESS;
}
Compile it with g++ and run it, you'll get:
用g ++编译并运行它,你会得到:
void describe(T&) [with T = int*[3]]
void describe(T&) [with T = int (*)[3]]
So, they are definitely NOT the same ! What you have is:
所以,他们肯定不一样!你有的是:
- an array of 3 pointers to int.
- a pointer to an array of 3 ints.
一个3个指向int的数组。
指向3个int数组的指针。
#4
6
It seems like your asterisks are lost in the formatting...
看起来你的星号在格式化中丢失了......
int *a[3]
declares an array of 3 int*
.
声明一个3 int *的数组。
int (*a)[3]
declares a as a pointer to a vector of ints. This is really not much different from any other pointer, it just points to a somewhat more complicated type.
将a声明为指向int的向量的指针。这与任何其他指针实际上没什么不同,它只是指向一个更复杂的类型。
int foo[3];
int bar[3];
int (*vp)[3];
vp = &foo;
(*vp)[0] = 0;
#5
3
There is an excellent article to be found at this URL on reading C type declarations. The author, Eli Bendersky, gives a simple method for reading the declarations. You start at the name of the variable and then move along the line pronouncing what you encounter as you walk along. The basic method is to start at the variable name and go right. I'll provide a simple overview, but I highly recommend you read the article.
在阅读C类型声明时,此URL上有一篇很好的文章。作者Eli Bendersky提供了一种阅读声明的简单方法。您从变量的名称开始,然后沿着发出您走路时遇到的内容的线移动。基本方法是从变量名开始并向右移动。我将提供一个简单的概述,但我强烈建议您阅读这篇文章。
- Start at the variable name and head right.
- If you hit a right paren
)
or a semicolon;
, then turn back to where you started going right and then go left. - If you encounter a left paren
(
while going right, then you have encountered a function declaration, and what follows is its comma-separated list of arguments. Note: you will encounter a right paren at the end of the argument list. The above rule does not apply to this right paren. - If you encounter a left bracket, read it as 'array'.
- After going left, when you hit a left paren
(
, then go back to the right paren where you last went right. Hop over that right paren, and then keep going repeat. - [Repeat]
从变量名开始,向右转。
如果你击中右边的paren)或分号;然后转回你开始向右走的地方然后向左走。
如果你遇到一个左paren(当你向右走,那么你遇到了一个函数声明,接下来是它的逗号分隔的参数列表。注意:你会在参数列表的末尾遇到一个正确的paren。上面的规则不适用于此权利paren。
如果遇到左括号,请将其读作“数组”。
向左走后,当你击中一个左边的paren(然后回到右边的paren,你最后一次右转。跳过那个右边的paren,然后继续重复。
So, in applying this rule to your particular problem...
因此,将此规则应用于您的特定问题......
In the declaration, " int * a[3];
", a
is the variable name. So, it is read:
在声明中,“int * a [3];”,a是变量名。所以,它被读取:
a
is an array ([
) of 3 elements ([3]
) of pointers (*
) to integers (int
)a是指针(*)到整数(int)的3个元素([3])的数组([)
While in the declaration, " int (* a)[3];
", a
is the variable name. So, it is read:
在声明中,“int(* a)[3];”,a是变量名。所以,它被读取:
a
is a pointer (*
) to an array ([
) of 3 elements ([3]
) of integers (int
)a是指向整数(3)的3个元素([3])的数组([)的指针(*)
#1
19
There is no difference between int a[3]
and int (a)[3]
, they both declare a
as an array of 3 int
s. There is a difference between int *a[3]
and int (*a)[3]
, the former declares an array of 3 pointers to int
whereas the second declares a pointer to an array of 3 int
s. The parenthesis make a difference here because in C brackets have a higher precedence than *.
int a [3]和int(a)[3]之间没有区别,它们都声明a为3个整数的数组。 int * a [3]和int(* a)[3]之间存在差异,前者声明了一个指向int的3个指针的数组,而第二个声明了指向3个int的数组的指针。括号在这里有所不同,因为在C括号中的优先级高于*。
#2
9
Alternatively, you can use cdecl
, which outputs the meaning of variable declarations in English.
或者,您可以使用cdecl,它以英语输出变量声明的含义。
cdecl> explain int*a[3]
declare a as array 3 of pointer to int
声明一个指向int的指针数组3
cdecl> explain int (*a) [3]
declare a as pointer to array 3 of int
声明一个指向int数组3的指针
#3
7
If you have any doubt, using this g++ trick is often handy:
如果您有任何疑问,使用这个g ++技巧通常很方便:
#include <iostream>
template < class T > void describe(T& )
{
// With msvc, use __FUNCSIG__ instead
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main(int argc, char* argv[])
{
int *a[3];
describe(a);
int (*b)[3];
describe(b);
return EXIT_SUCCESS;
}
Compile it with g++ and run it, you'll get:
用g ++编译并运行它,你会得到:
void describe(T&) [with T = int*[3]]
void describe(T&) [with T = int (*)[3]]
So, they are definitely NOT the same ! What you have is:
所以,他们肯定不一样!你有的是:
- an array of 3 pointers to int.
- a pointer to an array of 3 ints.
一个3个指向int的数组。
指向3个int数组的指针。
#4
6
It seems like your asterisks are lost in the formatting...
看起来你的星号在格式化中丢失了......
int *a[3]
declares an array of 3 int*
.
声明一个3 int *的数组。
int (*a)[3]
declares a as a pointer to a vector of ints. This is really not much different from any other pointer, it just points to a somewhat more complicated type.
将a声明为指向int的向量的指针。这与任何其他指针实际上没什么不同,它只是指向一个更复杂的类型。
int foo[3];
int bar[3];
int (*vp)[3];
vp = &foo;
(*vp)[0] = 0;
#5
3
There is an excellent article to be found at this URL on reading C type declarations. The author, Eli Bendersky, gives a simple method for reading the declarations. You start at the name of the variable and then move along the line pronouncing what you encounter as you walk along. The basic method is to start at the variable name and go right. I'll provide a simple overview, but I highly recommend you read the article.
在阅读C类型声明时,此URL上有一篇很好的文章。作者Eli Bendersky提供了一种阅读声明的简单方法。您从变量的名称开始,然后沿着发出您走路时遇到的内容的线移动。基本方法是从变量名开始并向右移动。我将提供一个简单的概述,但我强烈建议您阅读这篇文章。
- Start at the variable name and head right.
- If you hit a right paren
)
or a semicolon;
, then turn back to where you started going right and then go left. - If you encounter a left paren
(
while going right, then you have encountered a function declaration, and what follows is its comma-separated list of arguments. Note: you will encounter a right paren at the end of the argument list. The above rule does not apply to this right paren. - If you encounter a left bracket, read it as 'array'.
- After going left, when you hit a left paren
(
, then go back to the right paren where you last went right. Hop over that right paren, and then keep going repeat. - [Repeat]
从变量名开始,向右转。
如果你击中右边的paren)或分号;然后转回你开始向右走的地方然后向左走。
如果你遇到一个左paren(当你向右走,那么你遇到了一个函数声明,接下来是它的逗号分隔的参数列表。注意:你会在参数列表的末尾遇到一个正确的paren。上面的规则不适用于此权利paren。
如果遇到左括号,请将其读作“数组”。
向左走后,当你击中一个左边的paren(然后回到右边的paren,你最后一次右转。跳过那个右边的paren,然后继续重复。
So, in applying this rule to your particular problem...
因此,将此规则应用于您的特定问题......
In the declaration, " int * a[3];
", a
is the variable name. So, it is read:
在声明中,“int * a [3];”,a是变量名。所以,它被读取:
a
is an array ([
) of 3 elements ([3]
) of pointers (*
) to integers (int
)a是指针(*)到整数(int)的3个元素([3])的数组([)
While in the declaration, " int (* a)[3];
", a
is the variable name. So, it is read:
在声明中,“int(* a)[3];”,a是变量名。所以,它被读取:
a
is a pointer (*
) to an array ([
) of 3 elements ([3]
) of integers (int
)a是指向整数(3)的3个元素([3])的数组([)的指针(*)