简体   繁体   中英

What is the difference between Type** name, and Type* name[]?

What is the difference between Type** name, and Type* name[]?

Why would someone use one over the other?

Thanks

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.

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.

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:

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)

Also, when declaring global variables, you mustn't mix the two:

extern char* data[];

must be accompanied in the .c file by

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 */

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 .

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 .

Basically, Type** is a pointer to pointer. Think it like (Type*)* . So it points to Type* which can be a Type or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM