8

Possible Duplicate:
Declaring pointers; asterisk on the left or right of the space between the type and name?

I've always been wondering what's the exact correct position to put * and &. it seems that C++ is pretty tolerant about where to put these marks. For example I've seem pointer and ampersand been put on both left and right of a keyword or in the middle of two keywords, but confusingly sometimes they seems to mean the same thing, especially when used together with const

void f1(structure_type const& parameter)
void f2(structure_type const &parameter)

void f2(structure_type  const *sptr);
void f2(structure_type  const* sptr);
void f2(structure_type  const * sptr);

The examples are not exhaustive. I see them everywhere while being declared or been passed to a function. Do they even mean the same thing? But I also see cases while putting * will affect which object being referred as a pointer (likely the case where * is in between two keywords).

EDITED:

int const *Constant
int const * Constant // this above two seem the same to me, both as pointer to a constant value
int const* Constant //  EDIT: this one seems the same as above. instead of a constant pointer

const int * Constant // this is also a pointer to a constant value, but the word order changed while the pointer position stays the same, rather confusing.

int* const Constant
int * const Constant // instead, these two are constant pointers

So I concluded this:

T const* p; // pointer to const T
const T* p  // seems same from above
T* const p; // const pointer to T

Still, this confused the hell out of me. Doesn't the compiler care about position and the spacing required for them?

Edit: I want to know in general of the position matters. If yes in what cases.

Community
  • 1
  • 1
ryf9059
  • 729
  • 3
  • 14
  • 23
  • *this seems to mean the pointer itself is constant* -- http://ideone.com/okePlO – chris Jan 09 '13 at 03:58
  • Just don't use multiple pointer levels or your head will explode: `int const**const*const*const****const** wtf;` <- [Technically legal, but insane](http://ideone.com/2rZUd0). – Cornstalks Jan 09 '13 at 04:11
  • @chris [this link](http://www.parashift.com/c++-faq/const-ptr-vs-ptr-const.html) says it points to a constant field instead. But anyways the fact that the order of const can change confused the hell out of me already. – ryf9059 Jan 09 '13 at 04:20
  • 1
    @ryf9059, [Read this](http://c-faq.com/decl/spiral.anderson.html). It works wonderfully, but in this case, reading right to left is sufficient. – chris Jan 09 '13 at 04:21

3 Answers3

14

White space matters only to the degree that it keeps tokens from running together and (for example) creating a single token, so (for example) int x is obviously different from intx.

When you're dealing with something like: int const*x;, whitespace on either size of the * makes absolutely no difference to the compiler at all.

The difference between a pointer to const int and const pointer to int depends on which side of the * the const is on.

int const *x;    // pointer to const int
int *const x;    // const pointer to int

The primary difference is readability when/if you define/declare multiple objects in the same declaration.

int* x, y;
int *x, y;

In the first, somebody might think that x and y are pointers to int -- but in fact, x is a pointer to an int, and y is an int. To some people's eyes, the second reflects that fact more accurately.

One way to prevent any misinterpretation is to only ever define one object at a time:

int *x;
int y;

For any of these, correct interpretation is fairly easy if you ignore whitespace entirely (except to tell you where one toke ends and another starts, so you know "const int" is two tokens) and read from right to left, reading * as "pointer to". For example: int volatile * const x; is read as "x is a const pointer to a volatile int".

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    *Irrelevant nitpick to the theory being explained*: A constant pointer needs to be initialized. `int *const x;` will result in a compilation error and rightly so. – Alok Save Jan 09 '13 at 04:18
3
int const *Constant
int const * Constant 
int const* Constant

All of the above intend to declare a non constant pointer to a constant integer.

Simple rule:

If const follows after the * it applies to the pointer if not it applies to the pointed object. The spacing doesn't matter.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
-3

Both & and * placements in variable declaration are acceptable and only depends on your own flavour. They strictly mean the same thing, creating respectively a pointer and a reference.

However, the const keyword placement is primordial, as a int const* variable declares a constant pointer to a non-constant int, and const int* variable is a non-constant pointer to a constant int.

tsukasan
  • 118
  • 5