Dereference operator

This sounds like a newbie question but I need to ask.
Can you explain the difference between:


foo* bar;
foo * bar;
foo *bar;

Is there any difference and if so what difference does it make?
It doesn't make a difference to the compiler. You can even write it without space anywhere. foo*bar;

Some people prefer foo* bar; because foo* is the type of the variable so it makes sense to keep foo and * together.
I do that Peter87.

There is a caveat that newbies need to be aware of, though: the * is parsed attached to the variable name, so:

 
foo* a, b, c;

declares only one pointer-to-foo (‘a’); ‘b’ and ‘c’ are foos. Hence, when declaring multiple variables you must also repeat the *:

 
foo *a, *b, *c;

Best practice: avoid the issue. Either keep each variable declaration on its own line:

1
2
3
foo* a;
foo* b;
foo* c;

or use a typedef:

1
2
typedef foo* foo_ptr;
foo_ptr a, b, c;

Hope this helps.
C's declarations are supposed to reflect the usage of the thing being declared. Pointers have the indirection operator in front of them, because a "typical" use of the pointer uses the indirection operator. Functions are followed by parentheses because that's how functions are called, and arrays are followed by the subscript operator because that's how array elements are accessed.

While it's really not a big deal where you put the star, the caveat that @Duthomhas mentions starts to seem natural and even the most complicated declarators immediately become readable if you keep this in mind.

Were he still alive, I expect that Ritchie would put the star next to the variable name.
Last edited on
Dereference operator

The title and the question were entirely separate, until mbozzi pointed out the resemblance between declarations and use.

foo * bar; is a declaration. Not a dereference operator.
std::cout << *bar; is usage. Operators << and *.

void snafu( int ); A declaration
snafu( 42 ); Usage of function call<
(Classes can have operator() that allows them to be used like functions.)
Threads are regularly mis-titled regarding questions asked and content therein.
Threads are regularly mis-titled regarding questions asked and content therein.


Indeed, and when that happens, it's helpful to explain things to the OP, to help them understand things better.

If the OP here thinks that what they've used in their post is a dereference operator, then it is helpful for them to understand why it isn't, because that leads to a better understanding of how the * sumbol is used in different contexts.
...perhaps.

But perhaps the question doesn’t have anything to do with the name of the thing.


If I could re-title people’s posts, I’d name it

    Do spaces around the asterisk matter in pointer variable declarations?

I would not title it:

    Confusion about pointer declarator and whitespace 
    (And how this is not a question about the pointer indirection operator.)

That would be meaningless to every non-language-lawyer on the planet, and you shouldn’t have to be a language lawyer to use the stupid asterisk. (And anyone who could ask such a question doesn’t need to ask it.)
Topic archived. No new replies allowed.