0

Possible Duplicate:
In C, why is the asterisk before the variable name, rather than after the type?
Declaring pointers; asterisk on the left or right of the space between the type and name?

I have been using pointers for a while and used to declare pointers like

int *x;

But noticed thru some code that, the * seems to be after data type in some cases, like

void* setValue(){
 /*          */
}

What's the difference and why it's used like that

Community
  • 1
  • 1
Manikandaraj Srinivasan
  • 3,557
  • 5
  • 35
  • 62
  • 3
    Just a note: the compiler breaks the input stream into tokens, and then processes those. And white space isn't part of any token (although it can serve to separate tokens), so the token sequence seen by the parser in both cases is `int`, `*`, `x`, `;`. Regardless of whether it is written `int*x;`, `int *x;`, `int* x;` or `int * x;` (or even `int/*obfuscating*/*/*comment*/x;`---comments count as white space). – James Kanze Jul 17 '12 at 10:32
  • there is no difference at all. Either are used. – Sarfraz Ahmed Jul 17 '12 at 12:11

5 Answers5

13

Semantically nothing. It is style.

The advantage for putting the star right next to the variable is preventing this common error:

char* str1, str2;
/* str2 is a char, not char* ! */

char *str1, *str2;
/* ok */

This issue is non-existent with return types, so it matters even less there.

orlp
  • 112,504
  • 36
  • 218
  • 315
2

no difference, in some cases its better to keep it to the right:

int* p1,p2;

looks like p1 and p2 ara pointers, while only p1 is,

int *p1,p2;

now it looks a bit better, but good style is to split it to separate lines:

int *p1;
int p2;
marcinj
  • 48,511
  • 9
  • 79
  • 100
2

It's exactly the same as the difference between a+b and a + b - that is, there is no difference. Whitespace has no meaning at all, except when it's needed to separate tokens.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

No semantic difference.

Usually it is recommended to keep the * next to the type because the real type of x is void *. However this is syntactically false, since C/C++ binds the star to the variable name.

int *x, y; // declare one pointer to int x and one int y

Personally I try to keep the * in the middle

int * x;
log0
  • 10,489
  • 4
  • 28
  • 62
0

A pointer to void, noted as void* is a generic opaque pointer. You can pass it (as argument, result, thru variables, fields...) and compare it but the only way to get the pointed data is to cast it appropriately. An inappropriate cast is undefined behavior and can crash your program.

As for the space before or after the * denoting pointers in types, it is a style thing. Spaces are not really significant, except as token separator (and * is a token by itself).

I strongly suggest to be consistent with spacings. You could use tools like e.g. indent to indent your source code (and passing -T options to it help good indentation of type names). The GNU indent tool is normalizing spaces and has many options to tune its behavior (you might put them in a .indent.pro file). For C++ code, consider using astyle instead of indent.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547