views:

1828

answers:

10

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:

  1. int* test;
  2. int *test;
  3. int * test;
  4. int* test,test2;
  5. int *test,test2;
  6. int * test,test2;

Now, to my understanding, the first 3 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?

+37  A: 

4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:

int *test, *test2;

Or, even better (to make everything clear):

int* test;
int* test2;
Milan Babuškov
The winner by a nose. Upvote.
dmckee
So Case 4 is actually a death-trap then? Is there any specification or further reading that explains why int* test,test2 only makes the first variable a pointer?
Michael Stum
@ Michael StumIt's C++ so do you really think there is a logical explanation?
Joe Philllips
Ferruccio
Noted, pretty much everyone recommends that book, i'll grab a copy off Amazon then. But now at least I remember why I was so reluctant to pick up C/C++ in the Past :-)
Michael Stum
Cases 4, 5 and 6 are "death-traps". This is one reason why many C/C++ style gudes suggest only one declaration per statement.
Michael Burr
Whitespace is insignificant to a C compiler (ignoring the preprocessor). So no matter how many spaces there are or aren't between the asterisk and its surroundings, it has exactly the same meaning.
ephemient
+5  A: 

in 4, 5 and 6, test is always a pointer and test2 is not a pointer. White space is (almost) never significant in C++

1800 INFORMATION
+16  A: 

White space around asterisks have no significance. All three mean the same thing:

int* test;
int *test;
int * test;

The "int *var1, var2" is an evil syntax that is just meant to confuse people and should be avoided. It expands to:

int *var1;
int var2;
Ates Goral
+8  A: 

This is a good reason to use separate lines for each declaration. The * has to go with the variable name if you want it to be a pointer.

Joe Philllips
This answer is somewhat misleading in that it seems to be endorsing the idea that the whitespace between the asterisk and the type or variable name is significant.
jonner
+1  A: 

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.

Ron Warholic
+6  A: 

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.

int* test;   // test is a pointer to an int

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.

int* const test; // test is a const pointer to an int

int const * test; // test is a pointer to a const int ... but many people write this as  
const int * test; // test is a pointer to an int that's const
Scott Langham
+13  A: 

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).

Michael Burr
That looks daunting and quite horrible, sorry to say.
Joe Philllips
it does, but it seems quite a good explanation for some of the more complicated constructs
Michael Stum
@d03boy: There's no question - C/C++ declarations can be a nightmare.
Michael Burr
i like that rule very much.
Johannes Schaub - litb
+2  A: 

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:

int const bla;

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.

mstrobl
A: 

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:

int *test, *test2;

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:

-(-x) = x

is analogous to

*(*test) = test

This has always helped me. Sadly, the upshot of it all is that sometimes I use int* test and sometimes int *test.

wsd
Ben Voigt
the last statement was not C code (no semicolon), and just to illustrate what would happen if you declare int *test, and then use the dereference operator on it. In c it should be "int *test; int i = *test;". The double minus example is also no C code (again no semicolon) but a mathematical example, and there it works perfectly to my understanding.
wsd
A: 

I am sorry, but to me, int* p just looks ugly (and so does const int *p instead of int const *p).

If you have the jedi skill of parsing C++ types, int* p, as intuitive as it may look to some, makes some parts of the brain scream on the others - "this is not how this language works".

Pavel Radzivilovsky
You bumped an almost 2-year-old question just to write an opinion on style as an *answer*?
Potatoswatter