11

I'm just learning C++ and from all the example code I have looked at over the past few days I am having a hard time understanding where the pointer should be placed.

What's the difference in these 3?

1. char* x = /* ... */
2. char *y = /* ... */
3. char * z = /* ... */

Or

1. ClassX* x = /* ... */
2. ClassY *y = /* ... */
3. ClassZ * z = /* ... */

Thanks for your help.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user678994
  • 111
  • 3

3 Answers3

18

There's no difference. They're exactly the same.

You can choose to write it however you like. Typically, C++ programmers place the asterisk next to the type, while it is more common for C programmers to place the asterisk next to the name of the variable.

The only pitfall to be aware of is when you declare multiple variables on a single line (which you really shouldn't do anyway, if not for precisely this reason). For example, in the following statement, only variable x is declared as a pointer:

char* x, y;

Compare that with the following, which makes it much more clear which variables are pointers:

char *x, y;

As best I can tell, the third syntax emerged as a poor compromise between the two leading options. Instead of placing the asterisk next to one or the other, someone decided to place it in the middle, which is about the only place it decidedly does not belong.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    Personally, I use the third syntax for the same reason I put spaces between words when writing English, but that's just me. – Mike Seymour Mar 27 '11 at 16:21
  • @Mike: That's an interesting perspective, and one I'd never considered. As contentious as the argument is *which* piece of information the pointer modifies, I've never seen the argument that it's a piece of semantic information (a "word") all its own, perfectly capable of standing alone. – Cody Gray - on strike Mar 27 '11 at 16:29
  • I've never seen the argument that whitespace has any significance beyond separating tokens where necessary, and laying out code in a manner pleasing to the author. Some people seem to insist on reading some deep semantic meaning into it, however. – Mike Seymour Mar 27 '11 at 17:28
  • 1
    Personally, I use the first syntax for the same reason I append an 's' for plurals in English. The way you write reflects how you think about code, and some ways are more productive than others. – MSalters Mar 28 '11 at 08:59
  • 1
    Could I vote to mark this as an answer? Question's author seems to abandon StackOverflow in 2011. – Paul May 04 '21 at 14:04
2

no difference, but you must pay attention that if you write:

char* x, y;

only x is a pointer (the first variable declared) and you should reference them this way:

x = new (char);
*x = 'a';
y = 'b';
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alina Danila
  • 1,683
  • 1
  • 24
  • 60
0

Algough * is sometimes placed next to the variable it really does belong to the type declaration. So the following:

SomeType *x, *y;

really means

SomeType *x;
SomeType *y;

The same goes with &. Additionally *, const and & are applied in the order of their proximity to the declared type. For example:

const SomeType * const *&x = ...;

would mean: reference to pointer to a const pointer to const SomeType. const can be placed both before and after the type name. The const before the type is applied before all the modifiers after the type. Also note that this is not allowed:

const SomeType const x = ...;

as it would essentially mean const const SomeType, which doesn't make much sense.

julx
  • 8,694
  • 6
  • 47
  • 86
  • 1
    `(SomeType *)x;` can only be interpreted as a expression statement containing a cast expression, it cannot be a declaration so your "really means" is not correct. – CB Bailey Mar 27 '11 at 14:50
  • @Charles Well I wrote the remark about this not being a valid type declaration. Maybe it's not a good way of phrasing it. – julx Mar 27 '11 at 14:52
  • OK, perhaps I misunderstood exactly what you meant. – CB Bailey Mar 27 '11 at 14:55
  • @Charles I edited my answer. Removed the problematic bit and added some more information in place to make it more useful. – julx Mar 27 '11 at 15:06