Pages

Monday, December 28, 2009

Objective-C. Array.

Objective-C is still C language. And seems like all about the common C arrays works in Objective-C. Here is a small program that generates a table of Fibonacci numbers:
#import <Foundation/Foundation.h>

#define ARRAY_SIZE 15

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

int Fibonacci[ARRAY_SIZE], i;
Fibonacci[0] = 0; //by definition
Fibonacci[1] = 1;

for (i = 2; i < ARRAY_SIZE; ++i)
{
Fibonacci[i] = Fibonacci[i - 2] + Fibonacci[i - 1];
}

for (i = 0; i < ARRAY_SIZE; ++i)
{
NSLog(@"[%i] = %i", i, Fibonacci[i]);
}
[pool drain];
return 0;
}



The char array is the same char array:
#import <Foundation/Foundation.h>

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

char word[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
NSLog(@"%s", word);
[pool drain];
return 0;
}

As you see the size of the array is not mentioned in the array declaration. The array is initialized and, because, I'm going to print it in the console, I set the last array element as 0.


No comments:

Post a Comment