char* vs const char* as a parameter

后端 未结 5 489
逝去的感伤
逝去的感伤 2021-01-30 05:10

There are many times that I get compile errors when I use char* instead of const char*. So, I am not sure the actual difference, the syntax and the com

相关标签:
5条回答
  • 2021-01-30 05:48
    • char *       : non-constant pointer to non-constant character
    • const char *    : non-constant pointer to constant   character
    • char *const     : constant   pointer to non-constant character
    • const char * const : constant   pointer to constant   character


    Reference [link]

    0 讨论(0)
  • 2021-01-30 05:49

    If you're after the difference between the two, just think of them as:

    • char* is a pointer that points to a location containing a value of type char that can also be changed. The pointer's value can be changed, i.e. the pointer can be modified to point to different locations.
    • const char* is a pointer, whose value can be also changed, that points to a location containing a value of type char that cannot be changed.
    0 讨论(0)
  • 2021-01-30 05:50

    I always try to define parameters with const char* not char* because converting from std::string to conts char* is easy by .c_str() method. However converting std::string to char* is not that easy.

    0 讨论(0)
  • 2021-01-30 05:51

    Probably I'm too picky. In my book, the character(s) pointed to by a const char* can possibly be changed but not via the const char*. A const char * can point to modifiable storage. Example:

    char a[] = "abracadabra";
    const char * ccp = &a[0]; // ccp points to modifiable storage.
    *&a[0] = 'o'; // This writes to a location pointed to by const char* ccp
    

    So, my wording is:

    A char * is a pointer that be changed and that also allows writing through it when dereferenced via * or [].

    A const char * is a pointer that be changed and that does not allow writing through it when dereferenced via * or [].

    0 讨论(0)
  • 2021-01-30 05:54

    const char * means "pointer to an unmodifiable character." It's usually used for strings of characters that shouldn't be modified.

    Say you're writing this function:

    int checkForMatch(const char * pstr)
    

    You've promised (through the function signature) that you will not change the thing pointed to by pstr. Now say part of checking for a match would involve ignore the case of letters, and you tried to do it by converting the string to upper case before doing your other checks:

    strupr(pstr);
    

    You'll get an error saying you can't do that, because strupr is declared as:

    char * strupr(char* str);
    

    ...and that means it wants to be able to write to the string. You can't write to the characters in a const char * (that's what the const is for).

    In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's not a safe thing to do (passing something that isn't meant to be modified into something that may modify it).

    Of course, this is C, and you can do just about anything in C, including explicitly casting a const char * to a char * — but that would be a really, really bad idea because there is (presumably) some reason that the thing being pointed to by the pointer is const.

    0 讨论(0)
提交回复
热议问题