开发者

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

开发者 https://www.devze.com 2023-02-14 00:56 出处:网络
In order to print out something in file, I have the following code. FILE *fp = fopen(cString, \"w+\");

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

精彩评论

暂无评论...
验证码 换一张
取 消