Objective-c array passing
you can pass a C array to a method:
- (void) ookie: (int *) snork { NSLog (@"%d %d %d", snork[0], snork[1], snork[2]); } // ookie
Or use the [] syntax:
- (void) ookie2: (int[]) snork;
or use the explicit size syntax (which doesn’t really matter for 1-D arrays):
- (void) ookie3: (int[17]) snork;
And you can pass in pointers to pointers to pass back a resized array originally allocated by malloc:
- (void) ookie4: (int**) snork growTo: (int) elements { *snork = realloc(*snork, sizeof(int) * elements); (*snork)[0] = 55; NSLog (@"(%d) %d %d %d", sizeof(snork), (*snork)[0], (*snork)[1], (*snork)[2]); } // ookie4
Passing in arrays of Objective-C types:
- (void) ookie: (NSString **) snork { NSLog (@"%@ %@", snork[0], snork[1]); } // ookie
Log in to answer.
leothenerd 7:08 pm on December 14, 2009
you can pass a C array to a method:
Or use the [] syntax:
or use the explicit size syntax (which doesn’t really matter for 1-D arrays):
And you can pass in pointers to pointers to pass back a resized array originally allocated by malloc:
Passing in arrays of Objective-C types: