问题
Receiving the error Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'
On this line of the code
char* pos = strrchr (szPathName, '/');
The full code method for it is here;
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * path = [[NSBundle mainBundle] pathForResource: @"fd" ofType: @"dat"];
NSData* image = [NSData dataWithContentsOfFile:path];
const char* szPathName = [path UTF8String];
char* pos = strrchr (szPathName, '/');
*pos = 0;
if (CreateFaceFatted(szPathName) == 0)
{
NSLog(@"Init Dictionary failed");
}
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
回答1:
The error occurs because in C++, but not in C, strrchr is overloaded to return a const char * if its argument is a const char *. See Why strrchr() returns char* instead of const char*? for a discussion of this.
The solution is to follow the pattern in the preceding line, assign const char * values to variables of the same type.
来源:https://stackoverflow.com/questions/61663279/cannot-initialize-a-variable-of-type-char-with-an-rvalue-of-type-const-char