Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the star in C and C++ pointer notation [duplicate]

Tags:

c++

c

Possible Duplicate:
Correct way of declaring pointer variables in C/C++

For some time the following has been annoying me, where should I put the star in my pointer notation.

int *var; // 1 

and

int* var; // 2 

obviously do the same thing, and both notations are correct, but I find that most literature and code I look at use the 1th notation.

wouldn't it be more 'correct' to use the 2th notation, separating the type and the variable name by a whitespace, rather than mixing the type and variable tokens?

like image 382
Martin Kristiansen Avatar asked Dec 15 '12 16:12

Martin Kristiansen


People also ask

What does * and & indicate in 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 .

What does * After a variable mean in C?

When used with an already declared variable, the asterisk will dereference the pointer value, following it to the pointed-to place in memory, and allowing the value stored there to be assigned or retrieved.

What does double star do in C?

Two stars mean a pointer to a pointer.

What does * and & mean in C++?

Put simply. & means the address-of, you will see that in placeholders for functions to modify the parameter variable as in C, parameter variables are passed by value, using the ampersand means to pass by reference. * means the dereference of a pointer variable, meaning to get the value of that pointer variable.


1 Answers

No. Never. <g>

But consider:

int* var1, var2; 

Here, the placement of the * is misleading, because it does not apply to var2, which is an int and not an int*.

like image 169
Pete Becker Avatar answered Oct 04 '22 19:10

Pete Becker