Placement of the asterisk in Objective-C Placement of the asterisk in Objective-C objective-c objective-c

Placement of the asterisk in Objective-C


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;


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.


There is no difference — it's a matter of style. They all declare a variable called string that's a pointer to an NSString. The parentheses are necessary in some contexts (particularly method declarations) in order to clarify that it's a type declaration.