Well, the rough steps can be:
use count method of NSArray to know how many NSStrings are there in the NSArray.
use malloc to allocate memory for cargs, something like this
char **cargs = (char **) malloc(sizeof(char *) * count);
by your example, you may need to one more room for NULL which will be at the end of cargs.
use a loop and objectAtIndex: of NSArray to get out the NSStrings, like
NSString *nsstring = [array objectAtIndex:index];
use method cStringUsingEncoding: to get the c-string out, better make a copy
put these c-string pointers in cargs
pass cargs to your function, clean and free things needed to.
It's a lot of work. 'Cause the mix of c and obj-c stuff. And a lot of manual malloc and free , messy stuff. Can't you avoid it?
--add sample code--
I'm not quite sure what your real intent is. Hope this will help.
void func(char **arg)
{
int i;
for(i = 0; arg[i] != NULL; i++) {
printf("%d=%s\n", i, arg[i]);
}
}
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *s1 = [NSString stringWithString:@"first"];
NSString *s2 = [NSString stringWithString:@"second"];
NSString *s3 = [NSString stringWithString:@"third"];
NSArray *array = [NSArray arrayWithObjects: s1, s2, s3, nil];
//by now, we have an NSArray of three NSStrings
int count = [array count];
char **cargs = (char **) malloc(sizeof(char *) * (count + 1));
//cargs is a pointer to 4 pointers to char
int i;
for(i = 0; i < count; i++) {
NSString *s = [array objectAtIndex:i];//get a NSString
const char *cstr = [s cStringUsingEncoding:NSUTF8StringEncoding];//get cstring
int len = strlen(cstr);//get its length
char *cstr_copy = (char *) malloc(sizeof(char) * (len + 1));//allocate memory, + 1 for ending '\0'
strcpy(cstr_copy, cstr);//make a copy
cargs[i] = cstr_copy;//put the point in cargs
}
cargs[i] = NULL;
func(cargs);//call the function to do something
for(i = 0; i < count; i++) {
free(cargs[i]);
}
free(cargs);
[pool drain];
return 0;
}