Character Arrays
Program 13.2 illustrates how you can use a character array. However, one point is worthy of discussion. Can you spot it?
#import <Foundation/Foundation.h>int main (int argc, char * argv[]){ @autoreleasepool { char word[] = { 'H', 'e', 'l', 'l', 'o', '!' } ; int i; for ( i = 0; i < 6; ++i ) NSLog (@"%c", word[i]); } return 0;}
Hello!
In this case, the size of the array is determined automatically based on the number of initialization elements. Because Program 13.2 has six initial values listed for the array word
, the Objective-C language implicitly dimensions the array to six elements.
This approach works fine as long as you initialize ...
Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.