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 dis开发者_Python百科cards 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?
-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);
精彩评论