prosseek
prosseek

Reputation: 190659

Initialization discards qualifiers from pointer target type error in objective-c/cocoa

In order to print out something in file, I have the following code.

FILE *fp = fopen(cString, "w+");
NSString* message = [NSString stringWithFormat:@":SLEEP: %@:%@\n", ...];
char* cMessage = [message UTF8String]; <--  warning 
fprintf(fp, cMessage); <-- warning
fclose(fp);

However, I got Initialization discards qualifiers from pointer target type error in char* cMessage, and Format not a string literal and no format argument warning.

What's wrong with the code?

Upvotes: 1

Views: 2190

Answers (1)

Dave DeLong
Dave DeLong

Reputation: 243146

-UTF8String returns a const char *, but you're assigning it into a char *. As such, you're discarding the const qualifier.

As for fprintf, you should probably be doing:

fprintf(fp, "%s", cMessage);

Upvotes: 6

Related Questions