0

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;
}
user1695971
  • 103
  • 1
  • 5
  • 15
  • What do all of those square brackets around things mean? Like `NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];`? I'm not familiar with that. – JohnFilleau May 07 '20 at 16:57
  • 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 May 07 '20 at 17:02
  • 1
    @JohnFilleau Looks like Objective-C, to me? – Adrian Mole May 07 '20 at 17:34
  • Yes is Objective-C – user1695971 May 07 '20 at 17:49
  • With regard to tags, is this in a .mm file? .m files are Objective-C, and .mm files also respond to c++ stuff. If this is .m, then the C++ tag should not be there. – Almo May 07 '20 at 18:01
  • 1
    @Almo its a .mm file – user1695971 May 07 '20 at 18:02
  • If compiler asks to use `const char *`, then use it. Or better use auto type. – Cy-4AH May 08 '20 at 08:09

1 Answers1

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.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • thank you, although when I use the preding line and use this `const char* pos = strrchr (szPathName, '/');` The line is OK, however underneath the line `*pos = 0;` Returns an error "Read-only variable is not assignable" – user1695971 May 07 '20 at 22:50
  • Apologies, I didn't look ahead. The string returned by `UTF8String` is read-only and should not be altered, the type system is enforcing that. Now seeing what you are trying to achieve you will be better of extracting the part of the string you need before you convert to a C string - you might find `stringByDeletingLastPathComponent` does what you need. – CRD May 07 '20 at 23:47
  • Thank you, how may I implement that line into the code, I am struggling to find the area for it? – user1695971 May 07 '20 at 23:52
  • The original question (related to the error message) has been answered, if you want to ask a new question please open a new one - this is how SO works. In that question show what you've done, explain the issue etc. (remember SO expects you to show effort) and someone will undoubtedly help you along. – CRD May 08 '20 at 01:41