Correct way of declaring pointer variables in C/C++

C++CPointersCoding Style

C++ Problem Overview


I noticed some people use the following notation for declaring pointer variables.

(a) char* p;

instead of

(b) char *p;

I use (b). What is the rational behind the notation (a)? Notation (b) makes more sense to me because character pointer is not a type itself. Instead the type is character and the variable may be a pointer to the character.

char* c;

This looks like there is a type char* and the variable c is of that type. But in fact the type is char and *c (the memory location pointed by c) is of that type (char). If you declare multiple variables at once this distinction becomes obvious.

char* c, *d;

This looks weird. Both c and d are same kind of pointers that point to a character. In this since the next one looks more natural.

char *c, *d;

C++ Solutions


Solution 1 - C++

Bjarne Stroustrup said:

> The choice between "int* p;" and "int *p;" is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types. > > 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.

Source: http://www.stroustrup.com/bs_faq2.html#whitespace

I'd recommend the latter style because in the situation where you are declaring multiple pointers in a single line (your 4th example), having the asterisk with the variable will be what you're used to.

Solution 2 - C++

I personally prefer to place the * with the rest of the type

char* p;  // p is a pointer to a char.

People will argue "but then char* p, q; becomes misleading", to which I say, "so don't do that".

Solution 3 - C++

There are no difference how to write. But if you want to declare two or more pointers in one line better to use (b) variant, because it is clear what you want. Look below:

int *a;
int* b;      // All is OK. `a` is pointer to int ant `b` is pointer to int
char *c, *d; // We declare two pointers to char. And we clearly see it.
char* e, f;  // We declare pointer `e` and variable `f` of char type.
             // Maybe here it is mistake, maybe not. 
// Better way of course is use typedef:
typedef char* PCHAR;
PCHAR g, h;  // Now `g` and `h` both are pointers.
// If we used define construction for PCHAR we'd get into problem too.

Solution 4 - C++

The compromise is

char * p;

K&R uses

char *p;

It's up to you unless you're following a coding standard -- in that case, you should follow what everyone else does.

Solution 5 - C++

It's all a matter of preference, personally on projects that I see the char* I tend to declare multiple pointers on separate lines. There's no real "correct" way to do this and it all comes down to preference. Some say it's easier to read (a) while others say that (b) is easier to declare more variables of the same type on the same line.

I find (b) to be more common, and in some cases I have seen

char * a;

or something like this. Again preference. Whatever you're comfortable with or whatever the project I'm working on uses I will use (unless I write it myself in which case I use (a))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkeedaView Question on Stackoverflow
Solution 1 - C++BlackJackView Answer on Stackoverflow
Solution 2 - C++ikegamiView Answer on Stackoverflow
Solution 3 - C++George GaálView Answer on Stackoverflow
Solution 4 - C++jnnnnnView Answer on Stackoverflow
Solution 5 - C++Jesus RamosView Answer on Stackoverflow