101

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;
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
keeda
  • 2,605
  • 5
  • 28
  • 27
  • I agree with your reasoning and prefer (b) myself, but I never declare multiple variables on one line either, especially pointer variables. – Praetorian Aug 09 '11 at 02:19
  • 8
    What do you mean char pointer is not a type? Are you saying that a pointer is not a type? – Benjamin Lindley Aug 09 '11 at 02:21
  • 1
    I believe it is a matter of preference unless you are looking at a particular style guide (i.e. your company may have a certain coding standard you must follow). I tend to mix it up between choice b and some where in the middle. Sometimes I don't feel `char *t` looks proper, so I may do `char * t;` instead. However, I have frequently seen `char* t;` as well. – RageD Aug 09 '11 at 02:21
  • 1
    Also, your reasoning *"the type is char and *c (the memory location pointed by c) is of that type (char)"* would seem to indicate that there is a char being declared, when in fact, there is not. If it is treated as such, as it sometimes is by beginners, it will most certainly lead to memory access violations. – Benjamin Lindley Aug 09 '11 at 02:40
  • 1
    Both are still sufficiently descriptive, (b) shows *p is of type `char` and (a) shows p is a `pointer to a char` – A Person Jan 11 '14 at 21:31
  • [Where to put the star in C and C++ pointer notation](http://stackoverflow.com/questions/13894228/where-to-put-the-star-in-c-and-c-pointer-notation/13894275#13894275) – 0x90 May 10 '16 at 13:12
  • [Placement of the asterisk in pointer declarations](http://stackoverflow.com/q/180401/995714), [In C, why is the asterisk before the variable name, rather than after the type?](http://stackoverflow.com/q/398395/995714) – phuclv Aug 02 '16 at 10:03
  • "This looks like there is a type char* and the variable c is of that type." But each variable must have a type right, of what type variable c is then? How do you declare a vector/queue/whatever of pointers if pointer is not a type? – zypA13510 Oct 13 '16 at 10:12
  • 1
    With over a decade of industry experience, today was the first time this came up, and only in reading articles from other industry professionals. Obviously, then, I've never had any bit as a religious conversation about these as I have had with tabs v spaces, comments by def or dec, or any of the other "it's good to have nothing more important to argue about" topics. That being my perspective, I have preferred, so late in the game of my career, to go with "char * p" more often than not. Yes, space before, space after. Mostly as I find it reduces bugs, as it makes the pointer-ness more obvious. – Kit10 Oct 20 '16 at 16:54
  • Why not `char*p`? :) – 12Me21 Apr 03 '18 at 15:28

5 Answers5

116

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.

Community
  • 1
  • 1
BlackJack
  • 2,826
  • 1
  • 22
  • 25
  • 39
    Others recommend never to declare several objects in the same declaration. :-) – Bo Persson Aug 09 '11 at 03:53
  • I don't think it's at all true that a typical C programmer thinks he's declaring `*p` instead of `p`. – ikegami Aug 09 '11 at 05:09
  • 6
    I emphasize the logic behind the syntax, thus I prefer: `int *p`, meaning: when `p` is dereferenced I get an `int`. Then multiple declarations separated by `,` makes sense: `int *p, *q, r` (when `p` or `q` is dereferenced I get an `int`, when `r` is referenced I get an `int`). Also, the function pointer syntax makes sense: `int (*f)()` (when `f` is dereferenced and called I get an `int`). And this makes sense: `int *p, (*f)()`. – Druesukker Nov 19 '14 at 21:46
  • 4
    This helped me realize there is validity to the other side of the argument. Being, mostly, a high-level programmer, I emphasize type; and prefer `int*`. To me, `int *p, (*f)()` doesn't make much sense, but I can kind of see how it would to some people in some situations. – Assimilater Oct 24 '15 at 03:41
  • 1
    @Druesukker, your statement makes more sense than any of the above, I think. – Muntashir Akon Nov 23 '17 at 01:25
  • 1
    @Druesukker But the logic doesn't exactly work for `void*` (and doesn't work at all for C++ references). – jamesdlin Jan 25 '19 at 04:25
64

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".

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Very good argument for the char* p; choice :) I don't mind any of them as long as it doesn't become hard to read as the example you showed in which case they shouldn't be doing that. – Jesus Ramos Aug 09 '11 at 02:21
  • 1
    And when you want to output the value? You do `cout << *p;`. So why not putting the star _before_ `p` during declaration: `*p`? In my opinion it would be less confusing. – Evgenij Reznik May 29 '13 at 22:47
  • 17
    @user1170330, Because in once place it's part of the type definition, and it's a dereference operator in the other. No relation. If anything, it's a good thing that they look different, since different things should look different. – ikegami May 29 '13 at 23:28
37

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.
George Gaál
  • 1,216
  • 10
  • 21
  • 7
    `typedef` is not a better way "of course". Some people might prefer it, but it's problematic if there are multiple people working with your code. If I'm not used to `PCHAR`, I might write something like `PCHAR *p;`, which would be a `char**` and not what I intend. In general, if you want a `char**`, you'd have to write `PCHAR*` or define a new type, and then it just starts becoming stupid. – BlackJack Aug 09 '11 at 02:38
  • 4
    If one need some complex pointer type such as pointer to function, it is very good idea to make typedefs. They make code much simpler in this case. On the other hand, using PCHAR is more synthetical example than everyday practice. But it clearly makes what you want. *and then it just starts becoming stupid* Please direct all complaints to Stroustrup :-) – George Gaál Aug 09 '11 at 03:50
  • 1
    If you wrote PCHAR* p to define a pointer to char it would be obvious you don't know what you are doing and didn't read the type def. The answer is in his opinion. There's another benefit to the typedef; in C it's on the easy ways to make things opaque. publicly have the symbol defined as VOID ptr, privately the type def is something else.... I'd rather adjust a typedef once in a while then adjusting everything throughout the code when we change something about one of the types... but C++11 addresses much of that with auto's – UpAndAdam May 29 '13 at 22:31
  • For somebody just trying come back to toy with C++ five years after college, this is one of the best answers. – Panzercrisis Sep 19 '14 at 21:46
12

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.

jnnnnn
  • 3,889
  • 32
  • 37
  • 5
    I learned the hard way that char* x,y does not mean y is a char*. Now I can see why K&R follows the said style. – Rahul Kadukar Apr 05 '15 at 22:25
  • what is K&R and why does it matter what a single source of code uses? I use `char* p;`. it's a matter of style, not convention or rules. to me it makes more sense that p is of type `char*`, so obviously the * should be with the type. – FalcoGer Nov 01 '19 at 15:14
  • @FalcoGer I've added a link for K&R. It doesn't matter what a single source of code uses. Your team might get annoyed at you if you don't follow their standard because you are making them work harder to understand your code. These days, I would hope that teams are using `clang-format` anyway. – jnnnnn Nov 11 '19 at 04:09
2

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))

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88