4

Possible Duplicates:
In C, why is the asterisk before the variable name, rather than after the type?
What's your preferred pointer declaration style, and why?

In C++, i see pointers put in different ways. for example,

char* ptr
char * ptr
char *ptr

Are the 3 examples above identical? (same goes with the &)

Community
  • 1
  • 1
andrew
  • 83
  • 1
  • 5
  • 3
    See also: [this question](http://stackoverflow.com/questions/398395/in-c-why-is-the-asterisk-before-the-variable-name-rather-than-after-the-type), [this question](http://stackoverflow.com/questions/558474/what-makes-more-sense-char-string-or-char-string), [this question](http://stackoverflow.com/questions/2660633/declaring-pointers-asterisk-on-the-left-or-right-of-the-space-between-the-type-a) and [this question](http://stackoverflow.com/questions/377164/whats-your-preferred-pointer-declaration-style-and-why) – eldarerathis Apr 04 '11 at 21:28
  • syntactic and aesthetical sugar! – Tony The Lion Apr 04 '11 at 21:41

7 Answers7

7

Doesn't matter. (Eg. they are the same.) You could even write char*ptr; without any whitespace.
Beware though with multiple declarations on one line: char* ptr, noptr;. Here, the third syntax comes in clearer: char *ptr, noptr;.
My rule to avoid that confusion: Only one variable per line. Another way to do it right without the possibility to miss a *: typedef the pointer, eg:

typedef char* CharPtr;
CharPtr ptr1, ptr2; // now both are pointer

Though then you need to be aware of other things, like constness, so I stick to my rule mentioned above:

typedef char* CharPtr;
const CharPtr p1; // const char* ?? or char* const ??
CharPtr const p2; // char* const ?? or const char* ??
// Eg.: Can't the pointer or the pointee be changed?

(And all of the above also applies to references &.)

Xeo
  • 129,499
  • 52
  • 291
  • 397
4

C people tend to prefer char *p, whereas C++ people tend to prefer char* p (at least Bjarne does). The compiler could not care less about whitespace, you could just as well say char * p or char*p.

Many people say that char* p is dangerous because char* p, q is potentially confusing. That is correct in principle, but:

  1. To increase readability, you should not declare multiple names in one declaration, anyway.

  2. And more importantly, in C++, you should generally prefer RAII types over raw pointers.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
3

It does not matter. However, C++ people tend to prefer char* ptr, while C people prefer char *ptr. It's all a matter of personal preference.

However, note that char* ptr, noptr declares ptr as a pointer and noptr as a char value.

Håvard
  • 9,900
  • 1
  • 41
  • 46
  • 3
    That's pretty subjective, I like `char* ptr` more. – Xeo Apr 04 '11 at 21:24
  • @Xeo, it's of course a matter of personal preference as well, but most people I've talked to prefer it this way. – Håvard Apr 04 '11 at 21:26
  • @geofftnz: this declares a pointer to char and a char. Is this what you wanted ? – Alexandre C. Apr 04 '11 at 21:27
  • I always heard that `char *ptr;` is the "C-way" while `char* ptr;` is the "C++-way" (even my book said that! `;)`). – Xeo Apr 04 '11 at 21:28
  • 1
    @Xeo How do you feel about lines such as `char* ptr, val;`? I prefer `char* ptr`, but I end up doing `char* ptr; char val;` for multiple declarations. – tJener Apr 04 '11 at 21:28
  • @Alexandre typically it's not, that's what @geofftnz was pointing out. – corsiKa Apr 04 '11 at 21:28
  • @Alexandre: I think he wanted to hint that it doesn't declare two pointers. – Xeo Apr 04 '11 at 21:29
  • 1
    @tJener: My rule: One variable per line, only multiple variables when they are exactly the same, eg. all pointers or all references or none of them. – Xeo Apr 04 '11 at 21:29
  • 'char *ptr' stress that only ptr will be a pointer in case of multiple declarations. – BenjaminB Apr 04 '11 at 21:32
  • @Xeo Ah, yes, I meant that to be a multiline. What about `typedef`ing pointer types? – tJener Apr 04 '11 at 21:34
  • @tJener: That's a possibility, but like I said in my answers, you have to be aware of `const`ness: `typedef char* CharPtr; const CharPtr p1; CharPtr const p2;` What are those now? ;) – Xeo Apr 04 '11 at 21:36
3

C++ people emphasize the types, and C people emphasize the usage.

So you see char* x in C++, it is because "the type of x is pointer-to-char". C++ has a strong type system on which the language rests. This is not the case in C.

In C, you declare variables according to what you want to do with them. When you see char *x in C, the thought process is "when you dereference x, you get a char". Another example:

char (*f)(int);

reads "when you dereference f and call it with a int, you get a char", ie. f is a pointer to a function which takes int and returns char.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
2

They all work. char *ptr is often advocated because it makes it clearer what is happening in this case:

char *ptr, var;

This declares a char pointer and a char.

Null Set
  • 5,374
  • 24
  • 37
0

The potential whitespace surrounding the * and & modifiers in declaration have no effect on the compiled code.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

C++ is a free-form language. So, space doesn't really count in this case and all mean the same.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Yes. I like to write my code with either no (extra) white space. Or a "\n" wherever white space should be. Both make it interesting to read. – Martin York Apr 04 '11 at 21:43