无法使用类型为“const char *"的右值初始化“char *"类型的变量 [英] Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'

查看:25
本文介绍了无法使用类型为“const char *"的右值初始化“char *"类型的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Receiving the error Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'

在这行代码

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

推荐答案

发生错误是因为在 C++ 中,但 不是 在 C 中,strrchr 被重载返回一个 const char * 如果它的参数是 const char *.请参阅 为什么 strrchr() 返回 char*const char*? 的讨论.

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.

解决方案是按照上一行的模式,将const char *值赋给相同类型的变量.

The solution is to follow the pattern in the preceding line, assign const char * values to variables of the same type.

这篇关于无法使用类型为“const char *"的右值初始化“char *"类型的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆