1

I am new to c++ and i couldn't find anywhere what is the difference between when you put the '*' after the type or before the name. For example waht is the difference between the two:

int *p;
int* p;
A45
  • 165
  • 1
  • 8
  • 3
    For the compiler, there is no difference. For the reader, it's a question of style. The former tends to be how C programmers write the code, the latter tends to be how C++ programmers write the code. – Eljay May 22 '21 at 14:19
  • 3
    It is a coding preference. There is no difference. – Paul Ogilvie May 22 '21 at 14:19
  • @Eljay Never heard of this C vs C++ idea before. – HolyBlackCat May 22 '21 at 14:19
  • The philosophical question is whether the `*` attribute is a part of the type or of the variable. – Paul Ogilvie May 22 '21 at 14:20
  • 5
    both are the same as `int*p;` or `int * p ;` – pmg May 22 '21 at 14:21
  • 4
    The latter (with the `*` next to the type) can make it confusing when declaring multiple variables. With e.g. `int* p, q;` the type of `p` is `int*`, but the type of `q` is `int`, and `q` is *not* a pointer. – Some programmer dude May 22 '21 at 14:22
  • Thank you everyone for the answers, I now understand – A45 May 22 '21 at 14:24
  • Does this answer your question? [What makes more sense - char\* string or char \*string?](https://stackoverflow.com/questions/558474/what-makes-more-sense-char-string-or-char-string) or [Declaring pointers; asterisk on the left or right of the space between the type and name?](https://stackoverflow.com/questions/2660633/declaring-pointers-asterisk-on-the-left-or-right-of-the-space-between-the-type) – JaMiT May 22 '21 at 14:25
  • 2
    In C as well as in C++, multiple variables can be declared with one type: `int *p, i;` which makes a `int *p` and an `int i`. Writing this as `int* p, i;` looks like whether `p` and `i` are of type `int*` but that's not how the compiler does read it. Thus, it was a common style in the past to draw the `*` to the variable identifier. In parameters, where can be always only one parameter identifier after the type, this danger doesn't exist. This might be the reason why this new style became usual to move the `*` towards the type. (I always struggle with clang format which makes this by default.) – Scheff's Cat May 22 '21 at 14:25
  • 1
    @HolyBlackCat • [Stroustrup FAQ](https://www.stroustrup.com/bs_faq2.html#whitespace) – Eljay May 22 '21 at 14:33
  • Both forms are parsed as `int (*p);`. Whitespace is only significant to the extent it distinguishes tokens. Since `*` is never part of an identifier, the compiler can distinguish `int`, `*`, and `p` as separate tokens without any whitespace. Syntactically the `*` operator is always part of the declarator, not any type specifier. I personally do not like the `T* p` convention because it doesn’t follow the grammar and because it creates confusion. – John Bode May 22 '21 at 14:43

1 Answers1

1

C compiler ignores the whitespace (except whitespace inside character constants and string literals).

It means that

int    * p;
int*p;
int* p;
int *p;
int        *                                p                      ;

mean exactly the same.

The white space is only important in macros for example:

#define a(x) ((x)+(x))
#define a (x) ((x)+(x))

mean something completely different.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    C does not ignore whitespace, and it is not only important in macros. It separates tokens and is necessary where tokens would not otherwise be distinguished by the lexical rules. – Eric Postpischil May 22 '21 at 14:50
  • @EricPostpischil I did simplify it for the sake of the level of the question. People do not start learning math from complex numbers. Mind the OPs level. Do you think that citing 6.4 would explain anything to him? – 0___________ May 22 '21 at 15:35
  • 1
    We can teach people simple things before complicated things without lying to them. They can be taught the truth that `int *p` and `int* p` have the same meaning to the compiler (although they have different effects on humans) without telling them the falsehood that the compiler ignores white space. – Eric Postpischil May 22 '21 at 15:38
  • @EricPostpischil detailed understanding comes with knowledge and experience. – 0___________ May 22 '21 at 15:41
  • I did not ask for detailed understanding. I asked for truth. As I wrote, that can be a simple truth. Teaching a falsehood is bad; it creates something the student must unlearn in the future: They must find out they were told a falsehood, they must work to “forget” it, and they must learn the truth. It is harder, not easier. It is just easier on you to be sloppy and incorrect now, ignoring the future effects on students. Tell the truth. Do not tell lies. – Eric Postpischil May 22 '21 at 15:44