Pages

Tuesday, December 29, 2009

Objective-C. Pointers to Functions

Each day I see more and more nice and well-known C-things working in Objective-C.
Here is a program demonstrating how to work with the pointer to a function in Objective-C:

#import <Foundation/Foundation.h>

void hello(char str[])
{
NSLog(@"%s", str);
}

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

char word[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
hello(word);

const char* say = "hello, world!";
hello(say);

void (*fnHello)(char[]);
fnHello = hello;
fnHello("bye");

[pool drain];
return 0;
}

The screenshot:


No comments:

Post a Comment