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

发布于 2024-12-05 20:41:17 字数 1062 浏览 0 评论 0原文

可能的重复:
objc 警告:“丢弃指针目标类型中的限定符” < /p>

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

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 时,它似乎不正确。我看到这样的价值观:

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

Possible Duplicate:
objc warning: “discard qualifiers from pointer target type”

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());

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光与爱终年不遇 2024-12-12 20:41:17

理查德是正确的,这是一个使用 strdup 抑制警告。

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);

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);
回梦 2024-12-12 20:41:17

该警告是因为您传递了一个 char const ** (指针到指针到 const-char),而 API 需要一个 char ** (指针- to-pointer-to-char,特别是以 NULL 结尾的非常量 C 字符串列表)。
我的观点是 const 使指针类型不兼容。解决这个问题的安全方法是将 UTF8 字符串复制到非常量 C 字符串缓冲区中;不安全的方法是强制转换。

The warning is because you are passing a char const ** (pointer-to-pointer-to-const-char) where the API expects a char ** (pointer-to-pointer-to-char, in particular a NULL-terminated list of non-const C strings).
My point being the const makes the pointer types incompatible. The safe way to get around that is to copy the UTF8 strings into non-const C-string buffers; the unsafe way is to cast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文