0

Is there any easy way to convert an Objective-C holding class of NSStrings into parameters for a function accepting a variable list of char *? Specifically I have a function like:

-(void)someFunction:(NSSomething *) var

that I want to forward to a C function like

void someCFunction(char * var, ...)

Is there an easy way to go about this?

user1164112
  • 151
  • 2
  • 7

3 Answers3

1

No, you can only do what you want if the number of arguments you're passing is known at compile time. If you just want to convert a single string, use the -UTF8String message:

// Example with two strings
NSString *str1 = ...;
NSString *str2 = ...;
someCFunction([str1 UTF8String], [str2 UTF8String]);  // etc.

But if the number of strings will vary at runtime, you'll need to use a different API, if one is available. For example, if there's an API that took an array of strings, you could convert the Objective-C array into a C array:

// This function takes a variable number of strings.  Note: in C/Objective-C
// (but not in C++/Objective-C++), it's not legal to convert 'char **' to
// 'char *const *', so you may sometimes need a cast to call this function
void someCFunction(const char *const *stringArray, int numStrings)
{
    ...
}

...

// Convert Objective-C array to C array
NSArray *objCArray = ...;

int numStrings = [objCArray count];
char **cStrArray = malloc(numStrings * sizeof(char*));
for (int i = 0; i < count; i++)
    cStrArray[i] = [[objCArray objectAtIndex:i] UTF8String];

// Call the function; see comment above for note on cast
someCFunction((const char *const *)cStrArray, numStrings);

// Don't leak memory
free(cStrArray);
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
0

This would do the trick:

NSString *string = @"testing string"
const char * p1=[string UTF8String];
char * p2;
p2 = const_cast<char *>(p1);
HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • This seems to work for one string (unless I'm missing something). Is there a way to adapt this to a list of NSStrings or something? – user1164112 May 08 '12 at 02:38
  • please elaborate more, what do you mean list of string? do you mean NSMutableString? – HelmiB May 08 '12 at 02:41
0

Yes, this can be done, and is explained here:

How to create a NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects?

And here:

http://www.cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html

With modifications for ARC here:

How to create a NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects?

Also, variable arguments are not statically or strongly typed, as the other poster seems to be suggesting. In fact, there is no clear indication in the callee of how many arguments you really have. Determining the number of arguments generally breaks down into having to either specify the number by an count parameter, using a null terminator, or inferring it from a format string a la (s)print* . This is frankly why the C (s)print* family of functions has been the source of many errors, now made much much safer by the XCode / Clang / GCC compiler that now warns.

As an aside, you can approach statically typed variable arguments in C++ by creating a template method that accepts an array of an unspecified size. This is generally considered bad form though as the compiler generates separate instances for each size of array seen by by the compiler (template bloat).

Community
  • 1
  • 1
Chris Conover
  • 8,889
  • 5
  • 52
  • 68