无法使用 'const char *' 类型的右值初始化 'char *' 类型的变量



收到错误 无法使用类型为"const char *"的右值初始化类型为"char *"的变量

在代码的这一行上

char* pos = strrchr (szPathName, '/');

它的完整代码方法在这里;

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;
}

发生此错误是因为在 C++ 中而不是在 C 中,如果strrchr的参数是const char *则重载以返回const char *。请参阅为什么 strrchr() 返回char*而不是const char*?对此进行讨论。

解决方案是遵循前一行中的模式,将const char *值分配给相同类型的变量。

最新更新