0
votes

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
What do all of those square brackets around things mean? Like NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];? I'm not familiar with that.JohnFilleau
But strrchr will return a const char* if the input is a const char*, and a char * if the input is a char *. Any way to get szPathName as a char * instead of const char* automatically? If not, copy [path UTF8String] to a dynamically allocated char *. Or find some way to use std::string instead, which would be preferred.JohnFilleau
@JohnFilleau Looks like Objective-C, to me?Adrian Mole
Yes is Objective-Cuser1695971
@Almo its a .mm fileuser1695971

1 Answers

0
votes

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.