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?
As others mentioned, 4, 5, and 6 are the same. Often, people use these examples to make the argument that the
*
belongs with the variable instead of the type. While it's an issue of style, there is some debate as to whether you should think of and write it this way:or this way:
FWIW I'm in the first camp, but the reason others make the argument for the second form is that it (mostly) solves this particular problem:
which is potentially misleading; instead you would write either
or if you really want two pointers,
Personally, I say keep it to one variable per line, then it doesn't matter which style you prefer.
A good rule of thumb, a lot of people seem to grasp these concepts by: In C++ a lot of semantic meaning is derived by the left-binding of keywords or identifiers.
Take for example:
The const applies to the "int" word. The same is with pointers' asterisks, they apply to the keyword left of them. And the actual variable name? Yup, that's declared by what's left of it.
In 4, 5 and 6,
test
is always a pointer andtest2
is not a pointer. White space is (almost) never significant in C++.Many coding guidelines recommend that you only declare one variable per line. This avoids any confusion of the sort you had before asking this question. Most C++ programmers I've worked with seem to stick to this.
A bit of an aside I know, but something I found useful is to read declarations backwards.
This starts to work very well, especially when you start declaring const pointers and it gets tricky to know whether it's the pointer that's const, or whether its the thing the pointer is pointing at that is const.
White space around asterisks have no significance. All three mean the same thing:
The "
int *var1, var2
" is an evil syntax that is just meant to confuse people and should be avoided. It expands to:I would say that the initial convention was to put the star on the pointer name side (right side of the declaration
in the c programming language by Dennis M. Ritchie the stars are on the right side of the declaration.
by looking at the linux source code at https://github.com/torvalds/linux/blob/master/init/main.c we can see that the star is also on the right side.
You can follow the same rules, but it's not a big deal if you put stars on the type side. Remember that consistency is important, so always but the star on the same side regardless of which side you have choose.