5

Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
Correct C pointer notation

What is the difference between

int* x

and

int *x

(if one exists) ?

Community
  • 1
  • 1
Stumbler
  • 2,056
  • 7
  • 35
  • 61
  • That's great thanks! I'll accept one of the answers as soon as Stack Overflow allows me! – Stumbler Nov 29 '12 at 18:29
  • It's possibly a duplicate, but mine has a more specific name (for instance that question didn't appear when I did a quick search on the matter). – Stumbler Nov 29 '12 at 18:41

6 Answers6

8

They both the same. Only difference is that you cant declare many variables in such way:

int* x, a, b; //a and b are not pointers

int *x, *a, *b; //all are pointers

I use first notation, for me it shows that variable has pointer type, not pointer itself.

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
2

There is no difference. You will get in both cases an int pointer.

A reason to prefer the second one is when you declare multiple variables at once:

int *str, *foo;
int* str, foo;

Those two lines are different, the first one declares two pointers to int, whereas the second one declares one pointer to int and one int type variable.

Quazi Irfan
  • 2,555
  • 5
  • 34
  • 69
rekire
  • 47,260
  • 30
  • 167
  • 264
1

It's the same definition of pointer. both are pointer to an int

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
1

Whitespace has no significance to the compiler in this case. But, some people might interpret it as meaning differently (as in what points to what). Not a good thing to do in my opinion (since whitespace is not something that should be taken into great account).

Gustavo Litovsky
  • 2,457
  • 1
  • 29
  • 41
1

there is no difference at all.

In both case these are pointers to int

* is associated with identifiers not the type

just for clear vision best practice to give space between type and *.

int *p;

for e.g. if you write

int *p,q; means p is a pointer but q is int. it signifies that * is associated with identifier name

Omkant
  • 9,018
  • 8
  • 39
  • 59
1

They are the same, but you just have to be careful when declaring multiple variables:

 int* x, y; // x is a int*, while y is just an int
 int *z, *w; // both z and w are int*
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72