views:

99

answers:

3

Is there any reason why the asterisk is next to the object type in this code? I'm a little confused by the way I see this used. Some times it looks like this:

NSString* stringBefore;

and sometimes like this:

NSString *stringBefore;

Is there a difference? Or a right or wrong way to do this?

Thanks

+2  A: 

There is no difference.

texmex5
+4  A: 

It makes no difference.

It just just an indicator to how well versed the author is in writing and reading Objective-C. The traditional standard is to write it as:

NSString *stringBefore;
Paul Lynch
You can be well versed and choose not to follow the standard.
progrmr
That makes total sense to me, until the author does one of these:UISwitch *whichSwitch = (UISwitch *)sender;The parenthesis kind of throw it for me. Why is the asterisk grouped with the type?
HD-VRSCA
Because it is part of the type - as in, a "pointer to a NSString". And programmers who don't follow standards are called "bad programmers" - unless they have good reason, of course.
Paul Lynch
+10  A: 

I use the * near the variable name and not the type, since if you declare something like:

int *i, j;

i will be a pointer to int, and j will be a int.

If you used the other syntax:

int* i, j;

you may think that both i and j are pointers when they are not.

That said, I don't use nor recommend declaring a pointer and a non-pointer variable in the same line, as in this sample.

pgb