0

Around blogs and forums I noticed that people uses different ways to declare pointers in c++:

int* a = nullptr;

and

int *a = nullptr;

There's a difference between the two methods? If yes, what is?

2 Answers2

5

In most contexts in C++ white space is ignored, this being one of them. You can write it like int*a=nullptr;, int *a=nullptr;, int* a=nullptr;, int * a=nullptr; or whatever else suits you, it all means the same thing to the compiler. Most of it in people's preference comes down to a style guide usually or other forms of justification for which is most readable within their own codebase.

As others have pointed out as well, this may relate more to a misunderstanding on how types are determined on declarations, which in many cases can indeed be confusing. I suggest you look into learning how to read those (things like the spiral rule are a good place to start even if it's not 100% accurate) since that will help alleviate much of the confusion.

Lemon Drop
  • 2,113
  • 2
  • 19
  • 34
2

There's no difference. And the * allways apply only to a.

Example:

int *a= nullptr, b=12;   // a is a pointer, b is a plain int
int* a= nullptr, b=12;   // still a is a pointer and b an int, 
                         // despite misleading impression conveyed
Christophe
  • 68,716
  • 7
  • 72
  • 138