What is the difference between Type** name, and Type* name[]?
Type ** name和Type * name []有什么区别?
Why would someone use one over the other?
为什么有人会使用一个而不是另一个?
Thanks
4 个解决方案
#1
13
Well that depends, is it in a variable declaration or in a function argument? If in a variable declaration:
那取决于它是在变量声明还是在函数参数中?如果在变量声明中:
Type** name = &pointer_to_type;
Type* name[] = { &pointer_to_type, 0, &pointer_to_type };
The first one is a pointer to pointer to type, while the second one is an array of pointers to type of length 3.
第一个是指向类型指针的指针,而第二个是指向长度为3的类型的指针数组。
If in a function argument, they are the same thing. Arrays decay to pointers, and both Type** name
and Type* name[]
are exactly the same as function arguments. However, the second form makes it clear that name is an array of pointers of unknown length, while the first one does not. I would use Type**
to specify a single element and Type*[]
to specify an array.
如果在函数参数中,它们是相同的。数组衰减为指针,Type ** name和Type * name []与函数参数完全相同。但是,第二种形式清楚地表明name是一个未知长度的指针数组,而第一个不是。我会使用Type **来指定一个元素,使用Type * []来指定一个数组。
#2
4
The difference between the two is mostly demonstrated when declaring/defining objects of either type.
两者之间的差异主要在声明/定义任一类型的对象时得到证明。
The notation Type *name[]
creates an array of unknown size (can be inferred from the initializer), Type** name
creates a pointer. That means:
符号Type * name []创建一个未知大小的数组(可以从初始化程序推断),Type ** name创建一个指针。这意味着:
char *array[]={"hello", "world", 0}; /* OK, array of size 3 */
char **ptr={"hello", "world", 0}; /* not possible */
They behave differently in some expressions. Particularly, arrays can't be assigned to, but pointer variables can:
它们在某些表达中表现不同。特别是,无法分配数组,但指针变量可以:
ptr++, ptr=array; /* assignment and mutation of ptr possible */
// array=whatever /* impossible */
The sizeof operator works differently on the two. sizeof(array)
will depend on the number of elements of the array (may be 12 in this case), but sizeof(ptr)
returns always the same size (eg. 4 on main 32-bit architectures)
sizeof运算符在两者上的工作方式不同。 sizeof(array)将取决于数组的元素数量(在这种情况下可能是12),但sizeof(ptr)总是返回相同的大小(例如,在主32位体系结构上为4)
Also, when declaring global variables, you mustn't mix the two:
此外,在声明全局变量时,您不能混合使用两者:
extern char* data[];
must be accompanied in the .c
file by
必须在.c文件中附带
char* data[N];
and vice versa. Basically, defining the array means allocating several consecutive objects, whereas defining a pointer means allocating a single variable. The compiler treats both differently, and must know which is which.
反之亦然。基本上,定义数组意味着分配几个连续的对象,而定义指针意味着分配单个变量。编译器以不同的方式对待它们,并且必须知道哪个是哪个。
However, when declaring or passing parameters to functions, they are the same. So
但是,在向函数声明或传递参数时,它们是相同的。所以
int main(int argc, char** argv)
int main(int argc, char* argv[]) /* the same */
#3
1
Depends on the context.
取决于具体情况。
If it defines a variable which is not a function parameter, then in Type** name
, name
is a pointer to a pointer to a variable of type Type
and in Type* name[SOME_POSITIVE_INTEGER_CONSTANT]
, it's an array of pointers to variables of type Type
.
如果它定义了一个不是函数参数的变量,那么在Type ** name中,name是一个指向Type类型变量指针的指针,在Type * name [SOME_POSITIVE_INTEGER_CONSTANT]中,它是一个指向类型变量的指针数组类型。
If it's a function parameter, then both are the same, and name
is a pointer to a pointer to a variable of type Type
.
如果它是一个函数参数,则两者都是相同的,name是指向Type类型变量的指针。
#4
0
Basically, Type** is a pointer to pointer. Think it like (Type*)* . So it points to Type* which can be a Type or Type[].
基本上,Type **是指向指针的指针。想象它(类型*)*。所以它指向Type *,它可以是Type或Type []。
And the other one, Type* is a pointer to a Type or in this case, an array Type[]. So they are 'almost' the same.
而另一个,Type *是指向Type的指针,或者在这种情况下,是一个数组Type []。所以它们“几乎”相同。
#1
13
Well that depends, is it in a variable declaration or in a function argument? If in a variable declaration:
那取决于它是在变量声明还是在函数参数中?如果在变量声明中:
Type** name = &pointer_to_type;
Type* name[] = { &pointer_to_type, 0, &pointer_to_type };
The first one is a pointer to pointer to type, while the second one is an array of pointers to type of length 3.
第一个是指向类型指针的指针,而第二个是指向长度为3的类型的指针数组。
If in a function argument, they are the same thing. Arrays decay to pointers, and both Type** name
and Type* name[]
are exactly the same as function arguments. However, the second form makes it clear that name is an array of pointers of unknown length, while the first one does not. I would use Type**
to specify a single element and Type*[]
to specify an array.
如果在函数参数中,它们是相同的。数组衰减为指针,Type ** name和Type * name []与函数参数完全相同。但是,第二种形式清楚地表明name是一个未知长度的指针数组,而第一个不是。我会使用Type **来指定一个元素,使用Type * []来指定一个数组。
#2
4
The difference between the two is mostly demonstrated when declaring/defining objects of either type.
两者之间的差异主要在声明/定义任一类型的对象时得到证明。
The notation Type *name[]
creates an array of unknown size (can be inferred from the initializer), Type** name
creates a pointer. That means:
符号Type * name []创建一个未知大小的数组(可以从初始化程序推断),Type ** name创建一个指针。这意味着:
char *array[]={"hello", "world", 0}; /* OK, array of size 3 */
char **ptr={"hello", "world", 0}; /* not possible */
They behave differently in some expressions. Particularly, arrays can't be assigned to, but pointer variables can:
它们在某些表达中表现不同。特别是,无法分配数组,但指针变量可以:
ptr++, ptr=array; /* assignment and mutation of ptr possible */
// array=whatever /* impossible */
The sizeof operator works differently on the two. sizeof(array)
will depend on the number of elements of the array (may be 12 in this case), but sizeof(ptr)
returns always the same size (eg. 4 on main 32-bit architectures)
sizeof运算符在两者上的工作方式不同。 sizeof(array)将取决于数组的元素数量(在这种情况下可能是12),但sizeof(ptr)总是返回相同的大小(例如,在主32位体系结构上为4)
Also, when declaring global variables, you mustn't mix the two:
此外,在声明全局变量时,您不能混合使用两者:
extern char* data[];
must be accompanied in the .c
file by
必须在.c文件中附带
char* data[N];
and vice versa. Basically, defining the array means allocating several consecutive objects, whereas defining a pointer means allocating a single variable. The compiler treats both differently, and must know which is which.
反之亦然。基本上,定义数组意味着分配几个连续的对象,而定义指针意味着分配单个变量。编译器以不同的方式对待它们,并且必须知道哪个是哪个。
However, when declaring or passing parameters to functions, they are the same. So
但是,在向函数声明或传递参数时,它们是相同的。所以
int main(int argc, char** argv)
int main(int argc, char* argv[]) /* the same */
#3
1
Depends on the context.
取决于具体情况。
If it defines a variable which is not a function parameter, then in Type** name
, name
is a pointer to a pointer to a variable of type Type
and in Type* name[SOME_POSITIVE_INTEGER_CONSTANT]
, it's an array of pointers to variables of type Type
.
如果它定义了一个不是函数参数的变量,那么在Type ** name中,name是一个指向Type类型变量指针的指针,在Type * name [SOME_POSITIVE_INTEGER_CONSTANT]中,它是一个指向类型变量的指针数组类型。
If it's a function parameter, then both are the same, and name
is a pointer to a pointer to a variable of type Type
.
如果它是一个函数参数,则两者都是相同的,name是指向Type类型变量的指针。
#4
0
Basically, Type** is a pointer to pointer. Think it like (Type*)* . So it points to Type* which can be a Type or Type[].
基本上,Type **是指向指针的指针。想象它(类型*)*。所以它指向Type *,它可以是Type或Type []。
And the other one, Type* is a pointer to a Type or in this case, an array Type[]. So they are 'almost' the same.
而另一个,Type *是指向Type的指针,或者在这种情况下,是一个数组Type []。所以它们“几乎”相同。