Objective-C - 将NSString转换为C字符串

Objective-C  - 将NSString转换为C字符串

问题描述:


可能重复:

objc warning:“丢弃指针目标类型的限定符”

我在将 NSString 转换为C字符串时遇到了一些麻烦。

I'm having a bit of trouble converting an NSString to a C string.

const char *input_image = [[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String];
const char *output_image = [[[NSBundle mainBundle] pathForResource:@"iphone_resized" ofType:@"png"] UTF8String];

const char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL };

// ConvertImageCommand(ImageInfo *, int, char **, char **, MagickExceptionInfo *);
// I get a warning: Passing argument 3 'ConvertImageCommand' from incompatible pointer type.
ConvertImageCommand(AcquireImageInfo(), 2, argv, NULL, AcquireExceptionInfo());

此外,当我调试 argv 时,它没有'似乎是对的。我看到如下值:

Also when I debug argv it doesn't seem right. I see values like:

argv[0] contains 99 'c' // Shouldn't this be "convert"?
argv[1] contains 0 '\100' // and shouldn't this be the "input_image" string?


Richard是正确的,这是一个使用 strdup 以取消警告。

Richard is correct, here is an example using strdup to suppress the warnings.

char *input_image = strdup([@"input" UTF8String]);
char *output_image = strdup([@"output" UTF8String]);

char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL };

ConvertImageCommand(AcquireImageInfo(), 2, argv, NULL, AcquireExceptionInfo());

free(input_image);
free(output_image);