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

懵懂的女人 提交于 2021-01-29 05:41:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!