Objective-c nsarray to c array

Sorry, I don’t even know how to ask, since I am a complete newbie to C, pointers and the like. There's a function that takes an argument: char **arg. If I write this argument as follows:

char *cargs[] = {"blah", NULL};

and pass it to the function:

function(cargs);

it works. but ... I have NSArrayof NSStrings, and I need to make this array of values ​​from NSArray. I decided that I needed to create a C array from the same element as NSArray, and copy the strings, converting them using cStringUsingEncoding. But I honestly have no idea how to do this, since I got confused with all these pointers and the like. Any help would be appreciated.

+3
source share
2 answers

, :

  • count NSArray, , NSStrings NSArray.

  • malloc cargs, -

    char **cargs = (char **) malloc(sizeof(char *) * count);
    

    NULL, cargs.

  • objectAtIndex: NSArray, NSStrings, NSString * nsstring = [ objectAtIndex: index];

  • cStringUsingEncoding:, c-,

  • c- cargs

  • , .

. c obj-c. malloc , . ?

- -

, . , .

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;
}
+4

@yehnan GREAT - , , const ** cStyle[arrays] ... ( , - C ... , " " , - ...) " " ... " .."

char ** cArrayFromNSArray ( NSArray* array ){
   int i, count = array.count;
   char **cargs = (char**) malloc(sizeof(char*) * (count + 1));
   for(i = 0; i < count; i++) {        //cargs is a pointer to 4 pointers to char
      NSString *s      = array[i];     //get a NSString
      const char *cstr = s.UTF8String; //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;
  return cargs;
}
0

Source: https://habr.com/ru/post/1751163/


All Articles