-1

Possible Duplicate:
What's your preferred pointer declaration style, and why?
In C, why is the asterisk before the variable name, rather than after the type?
What makes more sense - char* string or char *string?

When declaring a new instance of an object in Objective-C, does it make any difference where you put the asterisk?

Is this just a matter of personal preference?

NSString* string = @"";

vs.

NSString *string = @"";
Community
  • 1
  • 1
MJN
  • 10,748
  • 1
  • 23
  • 32
  • 1
    This has been asked *many* times before: http://stackoverflow.com/questions/180401/c-asterisks-and-pointers http://stackoverflow.com/questions/2660633/declaring-pointers-asterisk-on-the-left-or-right-of-the-space-between-the-type-a http://stackoverflow.com/questions/558474/what-makes-more-sense-char-string-or-char-string http://stackoverflow.com/questions/377164/whats-your-preferred-pointer-declaration-style-and-why http://stackoverflow.com/questions/398395/in-c-why-is-the-asterisk-before-the-variable-name-rather-than-after-the-type to name a few – Adam Rosenfield Sep 13 '11 at 04:35
  • I believe this question is still important because it's likely to be found by Objective-C beginners who might not know to use the phrase "C pointer". – andyvn22 Sep 13 '11 at 04:37
  • @Adam Rosenfield - Which do you prefer? – MJN Sep 13 '11 at 05:43

3 Answers3

11

It doesn't make a difference, but there are good reasons to put it in each place:

  • It makes sense to put it near the class, because that makes it feel like a type: NSString*, a pointer to a string. Sensible.

  • It makes sense to put it near the variable, because that's what's actually happening: * is dereference. When you dereference your pointer string, you get an NSString. *string is an NSString. Sensible.

You may want to go with the latter because that's the way the compiler is thinking, so: NSString* oneString, anotherString will not work, whereas NSString *oneString, *anotherString is correct.

andyvn22
  • 14,696
  • 1
  • 52
  • 74
2

It's simply a matter of preference. Putting the * next to the type emphasizes that it's part of the type, i.e. "pointer to an NSString". However, this is usually frowned upon, because it ignores the fact that the * associates with the nearest variable name, not the type name. For instance, the following doesn't work:

NSString* a = @"string1", b = @"string2

This is because a is a pointer, but b is not.

Putting the * next to the variable name is, in my opinion, more of a C/C++ convention, because it emphasizes that the * and the variable name together act kind of like a variable.

Personally, I put a space on both sides of the *.

Another question that asked the same thing is here:

Declaring pointers; asterisk on the left or right of the space between the type and name?

Community
  • 1
  • 1
acjay
  • 34,571
  • 6
  • 57
  • 100
1

It doesnt make the difference wher you put that pointer symbol. If you declare multiple objects in single line, you do it like NSString *str1, *str2. So its more appropriate to put that asterisk close to object. I prefer it close to object instance.

jkr
  • 630
  • 1
  • 11
  • 24