Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

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

Why would someone use one over the other?

Thanks

like image 388
jmasterx Avatar asked Oct 04 '11 22:10

jmasterx


People also ask

What is the difference between name and type?

The name attribute is also a Global Attribute, a set of attributes that are available in all tags. The difference between <input type="email"> (which is valid HTML) and <input type="text"> is that in the first input, you cannot type anything other than an email address.

What is the difference between the * and & operators?

When a variable is paired with the * operator, that variable holds a memory address. When it is paired with the & operator, it returns the address at which the variable is held.

Is there any difference between int [] A and int a []? In C++?

There is no difference in functionality between both styles of declaration. Both declare array of int. But int[] a keeps type information together and is more verbose so I prefer it. They are semantically identical.

What does * and & indicate pointer?

The fundamental rules of pointer operators are: The * operator turns a value of type pointer to T into a variable of type T . The & operator turns a variable of type T into a value of type pointer to T .


3 Answers

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.

like image 102
K-ballo Avatar answered Oct 10 '22 18:10

K-ballo


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 */
like image 31
jpalecek Avatar answered Oct 10 '22 16:10

jpalecek


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.

like image 44
Alexey Frunze Avatar answered Oct 10 '22 16:10

Alexey Frunze