1

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?
Community
  • 1
  • 1
gotnull
  • 26,454
  • 22
  • 137
  • 203

3 Answers3

3

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);
Joe
  • 56,979
  • 9
  • 128
  • 135
2

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.

Richard
  • 3,316
  • 30
  • 41
  • I thought as much thanks to @LucasTizma's comments. How would I go about casting the API `char *` to a `const char *`? – gotnull Sep 21 '11 at 12:57
  • 1
    You will need to use something like `strdup` to copy the strings, then you will be responsible for freeing all duplicated strings. As I said in the other comment this is the fault of `ConvertImageCommand`. If this is your code the best way is to change the parameter to be `const char **`. – Joe Sep 21 '11 at 13:01
  • don't forget to `free()` the string `strdup` returns. – Richard Sep 21 '11 at 18:10
1

Use cStringUsingEncoding or getCString:maxLength:encoding:

Manlio
  • 10,768
  • 9
  • 50
  • 79
  • 2
    UTF8String also returns a C string. – CIFilter Sep 21 '11 at 12:26
  • @LucasTizma: See my updated code. I'm still receiving the warning. – gotnull Sep 21 '11 at 12:27
  • See http://stackoverflow.com/questions/329555/objc-warning-discard-qualifiers-from-pointer-target-type – CIFilter Sep 21 '11 at 12:34
  • @LucasTizma: If I'm not mistaken, I believe I am doing the exact same thing as that answer already? – gotnull Sep 21 '11 at 12:37
  • 1
    But you're declaring a C array of `char *`s, not `const char *`s, right? – CIFilter Sep 21 '11 at 12:39
  • I see, I see. I'm declaring the C array of `char *` because `ConvertImageCommand()` takes an array that is `char **`. So if I declare my array as `const char *` I get a warning: `passing argument 3 of 'ConvertImageCommand' from incompatible pointer type` – gotnull Sep 21 '11 at 12:42
  • 1
    That is the fault of `ConvertImageCommand()`, if `ConvertImageCommand()` does not modify the strings in the array in any way it should have declared it as a `const` – Joe Sep 21 '11 at 12:58