views:

104

answers:

2

In Objective-C, I've seen, for example:

UIPickerView *tweetPicker

and

UIPickerView* tweetPicker

What does the asterisk mean (I know, the first part is a dupe...) and why does it go on different parts of the deceleration in different contexts?

+1  A: 

The asterisk indicates that the variable is a pointer. In objective-C all objects are represented through pointers.

As far as where it is put, this is a style thing. My personal style is actually with spaces around the asterisk.

UIPickerView * tweetPicker

In all three cases they mean the same thing, but with different styles.

Edit: Spacing does not matter, but position does in corner cases dealing with keywords such as if const refers to a constant pointer or a pointer to a constant value. But your question was related to spacing, which does not matter.

Brandon Bodnár
Really? Position doesn't matter? Let me check it out. EDIT: Works, so why would "Headfirst iPhone Development" instruct me to do it the way I posted?
Moshe
@Moshe, the position does matter in some cases. I'll write up an answer.
Carl Norum
@Carl - thanks, I'm looking forward to reading it.
Moshe
What do you mean, "spacing" versus "position"? Isn't that the same?
Moshe
I have the Head First IPhone Development book, and I don't remember it every telling you to do it one way or the other. As I recall they actually use both styles you posted above, and Apple uses the style that I use. They are all legal and don't matter.
Brandon Bodnár
Position usually relates to order (Are you in first position or second), spacing normally relates to how much white space appears before or after something. C and Objective-C normally do not care about spacing except in cases where it is needed to tell the difference between tokens.
Brandon Bodnár
LOL - *everyone* here seems to have that book. +1 for the quickest answer. Carl was more thorough though. (If you must know why he got the check.)
Moshe
BTW, they don't tell you, they merely suggest it through usage.
Moshe
+3  A: 

In the exact case you're showing, there is no difference. Some people like to think of tweetPicker being of type UIPickerView *, that is, a pointer to a UIPickerView. These people usually write it as

UIPickerView* tweetPicker;

Other people prefer to think of it like *tweetPicker is a UIPickerView, that is, dereferencing the pointer gives a UIPickerView. Those people usually write:

UIPickerView *tweetPicker;

I prefer the latter, because the C (and Objective-C because of that) syntax supports it better. Take, for example, the following variable declarations:

int* a, b, c;
int  *a, *b, *c;

At first glance, the novice C (or Objective-C) programmer might say "those are the same", but they're not. In the first case, b and c are regular integers, in the second case, they're pointers. a is a pointer in both cases.

From my perspective, the concept of a "type" is so weak in C anyway (what with the behaviour of typecasting and the like) that extending that concept one step further to pointer variables is crazy - especially with the automatic & silent conversions to and from void * or id that you get.

Carl Norum
Thanks. Excuse the ignorance, but what's a "scalar"?
Moshe
Bad use of 'scalar', sorry - the C spec defines that. I'll edit.
Carl Norum
I'm just saying that I prefer the `*` with the variable name rather than the type. Doing it the other way can lead to confusing code (particularly for newcomers) because of the rules about how variable definitions work.
Carl Norum
@Carl - K thanks!
Moshe