1

I most commonly see code with the pointer next to the variable name instead of the type.

I originally preferred the later (·char* ch·) because to me it makes more sense that I am declaring the type as a character pointer and just naming the variable.

That said, I feel like I must be missing something if so many people see it the other way. What are other ways of looking at it that might provide more meaning?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Nate
  • 5,237
  • 7
  • 42
  • 52
  • 5
    Because the pointer-decl is married to the *variable*; not the *type*. `int* a,b` does not declare two pointers, though it certainly looks like it would. You can link it to the type using a typedef, `typedef int *intPtr; intPtr a,b;` but *don't*. It will just irk C programmers. We *want* to see the asterisks. – WhozCraig Nov 25 '13 at 06:17
  • Very good point WhozCraig. Thanks. – Nate Nov 25 '13 at 06:19

1 Answers1

5

If you write

char* ch;

or

char *ch; 

does not cause any issues.

However, if you need to declare multiple pointers,

char *ch1,*ch2; 

there is a chance you make a mistake like this,

char* ch1,ch2;

In this example, ch1 is a pointer but ch2 is plain int.

IMHO, it's better to use the * near the variable name to make sure you don't make a similar mistake

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59