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?
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 .
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.
Two stars mean a pointer to a pointer.
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.
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*
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With