Placement of the asterisk in Objective-C

I have just begun learning Objective-C, coming from a VB .Net and C# .Net background. I understand pointer usage, but in Objective-C examples I see the asterisk placed in several different places, and search as I might, I have not been able to find an answer as to why this is. Every search I try turns up all kinds of explanations about pointers (which I really don't need), but not a single mention of the reasons/effects of the different placements of the asterisk. Here are some examples I've seen:

NSString *string;
NSString * string;
(NSString *) string;
NSString* string;

What do these different positions of the asterisk mean? I'm sure it's a simple answer but it's frustrating not being able to find it in any of the Apple tutorial and reference documentation or online so far.

Can someone please end my misery and answer this perplexing question? Thanks!


Solution 1:

There is no difference, however you should be aware that only the first "token" (so to speak) defines the type name, and the * is not part of the type name. That is to say:

NSString *aString, bString;

Creates one pointer-to-NSString, and one NSString. To get both to be pointers, do either:

NSString *aString, *bString;

or:

NSString *aString;
NSString *bString;

Solution 2:

1.  NSString *string;
2.  NSString * string;
3.  (NSString *) string;
4.  NSString* string;

1, 2 and 4 are exactly identical. It's all style. Pick whatever you want, or mix it up.

Choice #3 has another meaning also, it's used in casting. For example:

t = (NSString *)string ;

will cast string to an NSString pointer.

But choice #3 is the syntax you'd probably use in a .h file or in the function definition in a .m file. Inside an actual function, in code which is "run" it has a different meaning.