-1

It might seems a noob question but I simply didnt find anywhere, and I'm curious about as a starter in C/C++. In what side I should put the asterisk? I've seen people using both declarations.

  • 5
    You're asking what difference a slight change of whitespace makes? None whatsoever. – Mike Seymour Feb 24 '15 at 18:37
  • 1
    It is just a style difference. – Jarod42 Feb 24 '15 at 18:38
  • Check out http://boredzo.org/pointers/ for more details. – childofsoong Feb 24 '15 at 18:38
  • why do people ask questions in the title. Why don't they ask it were they are suppose to! – Irrational Person Feb 24 '15 at 18:44
  • @IrrationalPerson, It's good to have a descriptive title, and a specific question is much more friendly than "C++ question help please". Of course, the one in the title should be simple and the body should elaborate and be unambiguous and clear by the time the question is actually asked. – chris Feb 24 '15 at 18:50
  • Both will be parsed as `char (*(argv[]))`. The `*` is part of the declarator, not the type specifier. The `[]` operator has higher precedence than the `*` operator, so it reads as "`argv` is an array of pointers to `char`", not "`argv` is a pointer to array of `char`". – John Bode Feb 24 '15 at 19:10
  • C style tends to put the `*` next to the variable name (because that reflects the way the syntax is defined). C++ style tends to put it next to the type name (because that's arguably more straightforward for simple cases, and because it's what Bjarne Stroustrup preferrs and used in the examples in his books). The compiler doesn't care. – Keith Thompson Feb 24 '15 at 19:26
  • @KeithThompson thanks for sharing a bit more of an info! – Renato Lins Feb 24 '15 at 22:00
  • In general, C bigots tend to prefer `type *var` rather than `type* var` because, in C, you can write `type *var1, var2` and then `var2` a `type`, not a `type*`. But I prefer `type* var` because it better emphasizes the "pointerness" of the item and because I never use the chained declaration style that creates the problem. – Hot Licks Feb 24 '15 at 22:07

1 Answers1

3

Nothing, though I prefer the latter version as I read * as "pointer".

So T* x; means x is a pointer to T.

Note that T* x, y; is the same as:

T* x;
T y;

Which is perhaps why some people recommend placing the * next to the variable name, but I personally just don't do multiple declarations on the same line because I think it's a bit ugly.

Clinton
  • 22,361
  • 15
  • 67
  • 163