Craig from Tejas
Craig from Tejas

Reputation: 1

Pointer declarations in Objective C

When reading various texts or code examples on Obj-C I see pointer declarations like this:

NSString *myStringPtr

but sometimes I see,

NSString * myStringPtr

where there is a space between the name and the *.

Is there a difference between these declarations or are they both just pointers to an object of type NSString or am I missing something?

In general is the space between the * and name necessary?

I have even seen NSString* myStringPtr

Is this any different than the above statements?

I realize (NSString *) for method return types or arguments means the method is returning (or being passed) a pointer to an NSString object.

Upvotes: 0

Views: 111

Answers (1)

mipadi
mipadi

Reputation: 410652

No, the asterisk can be attached to either the name or the type, or in between (with spaces as padding). It makes no difference. However, Objective-C most commonly uses the style

NSString *myStr;

rather than

NSString * myStr;

or

NSString* myStr;

The placement of the pointer does make a difference if you declare multiple variables one one line. For example, this

int* a, b;

is functionally identical to this:

int *a;
int  b;    /* Note: This is not a pointer! */

(Note: This also applies to C, as this is technically a C syntax issue and not specific to Objective-C.)

Upvotes: 1

Related Questions