I need to convert an NSarray filled with NSStrings and return this c array to the function.
-(char**) getArray{
int count = [a_array count];
char** array = calloc(count, sizeof(char*));
for(int i = 0; i < count; i++)
{
array[i] = [[a_array objectAtIndex:i] UTF8String];
}
return array;
}
I have this code, but when should i free the memory if I’m returning stuff?
You need to allocate memory for each string in the array as well.
strdup()would work for this. You also need to add aNULLto the end of the array, so you know where it ends:To free the array, you can use: