Placement of the asterisk in Objective-C

Objective CPointers

Objective C Problem Overview


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!

Objective C Solutions


Solution 1 - 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;

Solution 2 - Objective C

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.

Solution 3 - Objective C

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.

Solution 4 - Objective C

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

1,2 and 4 are equivalent. The C language (and the Objective-C superset of C) specify a syntax that is insensitive to white space. So you can freely add spaces where you choose as a matter of style. All relevant syntax is delimited by non-whitespace characters (e.g. {, }, ;, etc.) [1].

3 is either a type cast (telling the C compiler to use the NSString* type regardless of the declared type of string. In Objective-C, type casting of object instances is rarely necessary. You can use the id type for variables that can reference instances of any object type.

In method declarations, syntax 3 (sometimes without the ending semicolon) is used to declare the type of method parameters. An Objective-C method may look like this:

- (void)myMethodThatTakesAString:(NSString*)string;

In this declaration, the type of the argument named string is type NSString* (the leading - indicates an instance method as oppose to a class method). A method declaration with more than one parameter might look like this:

- (void)myMethodTakingAString:(NSString*)string andAnInteger:(NSInteger)intParam;

[1] This is compared to languages like Python which use whitespace as a block delimeter.

Solution 5 - Objective C

it doesn't matter where you put your asterisk, all statements create pointers of type NSString.

when using multiple variable names in one line you have to write the asterisk for each variable though.

NSString * nsstring, * nsstring2;

Solution 6 - Objective C

There is no difference, where the * is placed in a pointer declaration is irrelevant.

Solution 7 - Objective C

No difference, whitespace placement is irrelevant.

Solution 8 - Objective C

There is absolutely no difference. NSString* mystring and NSString *myString are identical.

Solution 9 - Objective C

there actually all equivalent: a pointer to an nsstring!!

Solution 10 - Objective C

There is absolutely no difference between these.

Solution 11 - Objective C

the * works the same way as it does in standard C.

this is a nice primer on putting the * in different places and what it does: http://boredzo.org/pointers/

Solution 12 - Objective C

1, 2 and 4 are equivalent and define a pointer to an NSString. My personal preference is to emulate K&R as much as possible, so I like to use NSString *string;

(NString*)string; though a valid statement, doesn't really do anything by itself.

$ cat foo.m
#include <Cocoa/Cocoa.h>

void foo()
{
    NSString *string;

    (NSString*) string;  // doesn't do anything
    42;   // doesn't do anything either
}

$ gcc -Wall -c foo.m
foo.m: In function 'foo':
foo.m:7: warning: statement with no effect
foo.m:8: warning: statement with no effect

Solution 13 - Objective C

in the xcode4 documentation sample code you can see 3. all the time, for example in MoveMeView.m

#if 1

- (void)growAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

#define MOVE_ANIMATION_DURATION_SECONDS 0.15

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS];
placardView.transform = CGAffineTransformMakeScale(1.1f, 1.1f);	
/*
 Move the placardView to under the touch.
 We passed the location wrapped in an NSValue as the context.
 Get the point from the value, then release the value because we retained it in touchesBegan:withEvent:.
 */
NSValue *touchPointValue = (NSValue *)context;
placardView.center = [touchPointValue CGPointValue];
[touchPointValue release];
[UIView commitAnimations];
}

Solution 14 - Objective C

There may not be any differences in the alternatives 1, 2 and 4 to the computer but there are to other readers of the code.

Since explanations like https://stackoverflow.com/a/1521382 and https://stackoverflow.com/a/16040884 and https://www.tutorialspoint.com/objective_c/objective_c_pointers.htm use the first alternative:

1. NSString *string;

and all extra variables need their own asterisk like in:

NSString *aString, *bString;

my humble suggestion is that we use alternative 1 as a standard.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjohn_5101View Question on Stackoverflow
Solution 1 - Objective CdcrostaView Answer on Stackoverflow
Solution 2 - Objective CmarccView Answer on Stackoverflow
Solution 3 - Objective CChuckView Answer on Stackoverflow
Solution 4 - Objective CBarry WarkView Answer on Stackoverflow
Solution 5 - Objective CknittlView Answer on Stackoverflow
Solution 6 - Objective CJohn MillikinView Answer on Stackoverflow
Solution 7 - Objective CJStriedlView Answer on Stackoverflow
Solution 8 - Objective CDarylView Answer on Stackoverflow
Solution 9 - Objective CennuikillerView Answer on Stackoverflow
Solution 10 - Objective CconeybeareView Answer on Stackoverflow
Solution 11 - Objective CpxlView Answer on Stackoverflow
Solution 12 - Objective CsigjuiceView Answer on Stackoverflow
Solution 13 - Objective Cme1974View Answer on Stackoverflow
Solution 14 - Objective C1-2-CView Answer on Stackoverflow