I’m trying to pass an NSString to a C function, although it seems to not accept (or ignore) the parameters. Any help would be appreciated – thanks.
int copyfiles(int argc, const char **argv)
{
if(argc < 2 || argc > 3)
{
puts("usage: copy file [outfile]");
return 1;
}
const char *infile = argv[1];
char *outfile;
if(argc > 2)
{
outfile = strdup(argv[2]);
expect(outfile, "allocate");
}
...
}
@implementation MyApplication
@synthesize window;
- (void)copy:(NSString *)pathToFile
{
NSString *pathToFile = @"/path/to/file";
copyfiles((int)(const char *)[pathToFile UTF8String],(const char **)[pathToFile UTF8String]);
}
I don’t get any errors, but the output gives me “usage: copy file [outfile]”, so I’m obviously not casting the parameters correctly.
Have a look at your call to
copyfiles, specifically why you’re passing a string to a function that wants an integer for its first argument.You need to pass that function an argument count followed by the pointer-to-pointer for the argument list.
For example, you would call it with the following C code (untested but you should get the general idea):
The first line creates an array of character pointers (more correctly, C strings) including the NULL at the end which is mandated by the ISO C standard.
The second line passes two arguments, the first being the size of the array minus one (the number of “real” arguments in the list) and the second being the array itself.
In your particular case, where you seem to be using the one-filename variety, you should start with something like:
since your C function expects the
main-like behaviour where the first argument is the “program” name.