Functions as pointers in Objective-C

Posted by richman0829 on Stack Overflow See other posts from Stack Overflow or by richman0829
Published on 2010-05-29T03:24:04Z Indexed on 2010/05/29 3:32 UTC
Read the original article Hit count: 374

This is a question from Learn Objective-C on the Mac...

Functions as pointers

What I typed in, as per the recipe, was:

NSString *boolString (BOOL yesNo) {
if (yesNo) { return (@"YES");
} else { return (@"NO");
} } // boolString

The pointer asterisk in the first line doesn't seem necessary, yet deleting it results in an error message. But what does it do? In

 NSString * boolString (yesNo);

what seems to be going on is a function is defined as a pointer to an NSString. The function without the asterisk

NSLog (@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent));

returns an NSString of YES or NO. But how can it return an NSString when it's a pointer? It might return the ADDRESS of an NSString; or if dereferenced it could return the CONTENTS of that address (an NSString such as YES or NO). Yet I see no place where it is dereferenced.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about function-pointers