I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition.
How about these examples:
int* test;
int *test;
int * test;
int* test,test2;
int *test,test2;
int * test,test2;
Now, to my understanding, the first three cases are all doing the same: Test is not an int, but a pointer to one.
The second set of examples is a bit more tricky. In case 4, both test and test2 will be pointers to an int, whereas in case 5, only test is a pointer, whereas test2 is a "real" int. What about case 6? Same as case 5?
4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:
Or, even better (to make everything clear):
The rationale in C is that you declare the variables the way you use them. For example
says that
*a[42]
will be achar
. Anda[42]
a char pointer. And thusa
is an array of char pointers.This because the original compiler writers wanted to use the same parser for expressions and declarations. (Not a very sensible reason for a langage design choice)
Cases 1, 2 and 3 are the same, they declare pointers to int variables. Cases 3, 4 and 5 are the same, as they declare one pointer to, and one int variable respectively. If you want do declare two pointers in one line (which you shouldn't), you need to put an asterisk in front of each variable name:
There is no certain correct way that says where the asterisk goes.
int* test
looks better because it is easier for us to imagine that appending*
to the end of a type means "pointer to" that type. However,int *test
makes more sense, because you can work with it like the minus sign in maths:is analogous to
This has always helped me. Sadly, the upshot of it all is that sometimes I use
int* test
and sometimesint *test
.Use the "Clockwise Spiral Rule" to help parse C/C++ declarations;
Also, declarations should be in separate statements when possible (which is true the vast majority of times).
The pointer is a modifier to the type. It's best to read them right to left in order to better understand how the asterisk modifies the type. 'int *' can be read as "pointer to int'. In multiple declarations you must specify that each variable is a pointer or it will be created as a standard variable.
1,2 and 3) Test is of type (int *). Whitespace doesn't matter.
4,5 and 6) Test is of type (int *). Test2 is of type int. Again whitespace is inconsequential.