Sorry, I’m not even sure how to ask, since I’m a complete newbie at C, pointers and stuff like that. There’s a function that accepts an argument: char **arg. If I write that argument like so:
char *cargs[] = {"blah", NULL};
and pass it to the function:
function(cargs);
it works. but … I have an NSArray of NSStrings and I need to make this array out of values from NSArray. I figured it should be a matter of creating a C array of the same element count as NSArray and copy the strings, converting them with cStringUsingEncoding. But I honestly have no idea how to do this, since I get confused with all those pointers and such. Any help would be appreciated.
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
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.