25

It's pretty common knowledge that the semantically accurate way to declare pointers is

int *x;

instead of

int* x;

This is because C sees *x as an int, not x as an int pointer.

This can be easily demonstrated by

int* a, b;

where a is an int pointer, while b is an int.

There are at least 5 duplicate questions on Stack Overflow that discuss this issue for pointers. But what about references?

11
  • 3
    Spaces don't matter, as they are skipped during the lexing phase. You can have zero or more space before and zero or more spaces after the asterisk. They both declare x as a pointer, not a reference. Mar 31, 2014 at 16:34
  • 1
    @mic_e: That's exactly what that button is there for. We even have a post on meta demonstrating it: meta.stackexchange.com/questions/132886/… Unfortunately this is often seen as an attempt to gain more rep, even though answering your own question is certainly encouraged.
    – BoltClock
    Mar 31, 2014 at 16:35
  • 2
    In C++ it is considered good style to put the pointer (*), reference (&) or other type modifiers together with the type and not right before the name. This is because int& i separates names and types better than int &i. See the articles on isocpp.org, Herb Sutters or scott Meyers for examples.
    – Jens
    Mar 31, 2014 at 17:03
  • 2
    @Jens: It's pretty horrible, if you think about it... C++ follows a completely different design philosophy than C, prompting int& i as good style, but due to its legacy C compatibility that 'good style' directly cloaks the actual semantics of your code...
    – mic_e
    Mar 31, 2014 at 17:15
  • 6
    Handling multiple declarations in that way was a very bad design choice when creating the C language. Now, we are stuck with it, in C++ too. It's true that using int *i better matches that language-semantics rule, however, it's a terrible rule. The better way to deal with it is to (1) not allow multiple declarations and (2) distinguish the type from the identifier (e.g. int* i or int& i). Anyone with the least bit of knowledge in C/C++ knows about this rule, and so, we don't need to remind people of it, we just need to avoid it. Mar 31, 2014 at 17:55

3 Answers 3

27

Bjarne Stroustrup says:

A typical C programmer writes int *p; and explains it *p is what is the int emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A typical C++ programmer writes int* p; and explains it p is a pointer to an int emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

When declaring a pointer variable or argument, you may place the asterisk (or ampersand) adjacent to either the type or to the variable name.

The most important thing is to do this consistently within a single file.

// These are fine, space preceding.
char *c;
const int &i;

// These are fine, space following.
char* c;
const int& i;
12

While researching for this question, I already found the answer:

The & needs to be written just like the *.

The demonstration code is similar to the pointer demonstration code:

int main() {
    int a = 0;
    int b = 1;

    int& ar = a, br = b;

    br = 2;

    return b;
}

This returns 1, which means that ar is an int reference, while br is just an integer.

3
  • 9
    Many style guides suggest one variable per declaration. Although more than one is possible, readability is improved when it comes to pointer and reference declaration. Mar 31, 2014 at 16:35
  • 2
    @ThomasMatthews: Definitely. In no way I would encourage actually declaring multiple pointers, references or even variables in one statement. This is just a demonstration of the internal workings of C++.
    – mic_e
    Mar 31, 2014 at 16:44
  • As this code's result demonstrates, commas in C++ variable declarations have a nuanced meaning that is different from their meaning in other languages and isn't necessarily intuitively obvious. I agree that they should be avoided. However, I do prefer to place a space either before or both before and after an ampersand.
    – Shavais
    Jun 25, 2020 at 21:27
11

Thanks to "template typedefs", you can declare multiple references in an (arguably) nicer way:

template<typename T> using ref = T&;
int a, b;
ref<int> ar = a, br = b;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.