The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double) too. Naming Convention of Pointers: Include a "p" or "ptr" as prefix or suffix, e.g., iPtr, numberPtr, pNumber, pStudent.
The asterisk * used to declare a pointer is the same asterisk used for multiplication.
When declaring a pointer type, place the asterisk next to the type name. Although you generally should not declare multiple variables on a single line, if you do, the asterisk has to be included with each variable.
Why does C use the asterisk for pointers? Simply - because B did. Because memory is a linear array, it is possible to interpret the value in a cell as an index in this array, and BCPL supplies an operator for this purpose. In the original language it was spelled rv , and later ! , while B uses the unary * .
It's a matter of preference, and somewhat of a holy war, just like brace style.
The style
someType* somePtr;
is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr
is pointer-to-someType
".
The style
someType *somePtr
is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr
is someType
".
They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.
Putting it in the middle (as someType * somePtr
) is trying to avoid committing to either one.
It doesn't matter. Someone will now come along and close the question as a dupe, and someone else will show how the int* a
way breaks if you declare multiple variables in the same declarations while int *a
better reflects the syntactical structure of the code, and another one will show that Stroustrup prefers the int* a
way and keeps the type together on the left side.
Many opinions, but no "right" way here.
It doesn't matter, it is personal preference.
Some people like to keep the type together:
int* p;
Other people say that it should go next to the variable because of the following:
int *p, x;//declare 1 int pointer and 1 int
int *p, *x;//declare 2 int pointers.
Over time you will just overlook this and accept both variations.
The difference arose because C++ added a stronger type system on top of C. A C programmer usually thinks in terms of "values," so
int *pValue;
reads "the dereference of pValue is an int" whereas a C++ programmer thinks in "types" so
int* pValue;
reads "the type pValue is pointer to int" The compiler sees no difference at all of course. However you will find that it is the C programmer who insist on "value semantics" when programming in C++.
I think putting the asterisk adjacent to the name of the variable is clearer.
You might mistakenly declare someType* one, two;
thinking that they are both pointers but only the variable one
is a pointer; two
is just a someType
. Declaring as someType *one, *two
avoids this problem.
Every single way I've seen ever is
TheType *myPointer
because you are declaring a POINTER of type TheType. Similar declaring
TheType myVar
would be declaring an instance variable of type TheType.
Also you can then clearly do this and have it easily readable
TheType myVar, *myPointer;
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