3

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

I am fighting with the c language. Pointers are new to me, and I think I am getting closer and closer to understanding them.

I have though one questions.

What is the difference between:

int k = 4;
int* pcp = &k;

and

int k = 4;
int *pcp = &k;

I cant seem to find any difference between these declarations of the pointer, is it just syntactical sugar - or is there any difference?

Thanks

Community
  • 1
  • 1
user1090614
  • 2,575
  • 6
  • 22
  • 27

2 Answers2

5

There is no difference in those declarations, but there is a difference between the following two declarations:

int* p, p2;  // declares a pointer to int and a regular int 

and:

int *p, *p2; // declares two pointers to int

that might be hidden by your example.

So I prefer the second declaration.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 2
    That's why it's usually preferred to glue the `*` to the variable name -- `int* p, p2;` is painful. – David Schwartz Sep 27 '12 at 17:32
  • @DavidSchwartz No, it is usually preferred to use separate declarations for each pointer, and to glue the '*' to the base type because that's where it belongs. – Jim Balter Sep 27 '12 at 17:52
-2

you can try it out yourself. just type both, printf it and see what happens :P

if i'm not terribly mistaken though, it's the same ;)

foaly
  • 342
  • 2
  • 3
  • 10