Why I get; initializing 'char *' with an expression of type 'const char *' discards qualifiers?

后端 未结 6 1144
陌清茗
陌清茗 2021-01-05 05:37

I can\'t figure out why I get this warning from clang by myself:

function_prototype_const_modifier.c:13:8: warning: initializing \'char *\' with         


        
相关标签:
6条回答
  • 2021-01-05 06:08

    In this case, just about what's we can do.

    Thanks of clearness information for @user529758.

    Just plus a answer.

    Modified:

    #include<stdio.h>
    
    char *my_strcpy(char *destination, const char *source);
    
    int main(void) {
        char str1[] = "this is something";  
        char str2[] = "123456789123456789";
        my_strcpy(str2, str1);
        puts(str2);
        return 0;
    }
    char *my_strcpy(char *destination, const char *source) {
        char *ptr1 = (char*)source;
        char *ptr2 = destination;
        while(*ptr1 != '\0') {
            *ptr2++ = *ptr1++;
        }
        *ptr2 = '\0';
        return destination;
    }
    
    0 讨论(0)
  • 2021-01-05 06:13

    You just need to change:

    char *ptr1 = source;
    

    to:

    const char *ptr1 = source;
    
    0 讨论(0)
  • 2021-01-05 06:14

    Because type of L.H.S is char * and type of R.H.S is const char *.

    The reason is exactly as what error says:

    function_prototype_const_modifier.c:13:8: warning: initializing 'char *' with an expression of type 'const char *' discards qualifiers

    The statement allows you to discard the const qualifier and it allows to modify the pointed string through ptr1 and ptr2 and hence the compiler complains.

    0 讨论(0)
  • 2021-01-05 06:19

    You are assigning a pointer to a character constant to a pointer to a char. By doing that you risk modifying the character(s).

    0 讨论(0)
  • 2021-01-05 06:21

    source is a const char *, a pointer to const characters, so the characters cannot be changed by dereferencing the pointer (i. e. source[0] = 'A'; is a constraint violation).

    However, assigning it to a char * discards this constraint; a simple char * suggests that the characters pointed to by the ptr1 pointer are not constant and you can now freely write ptr1[0] = 'A'; without getting compiler errors (a "diagnostic message").

    Consider what this means when you pass in a string literal. Since a string literal is "readonly" (it's a const char []), trying to modify its contents is undefined behavior. So if you call

    my_strcpy(destination, "Constant String");
    

    but in the code for some reason you write

    ptr1[0] = 'A';
    

    you won't get a compiler diagnostic message because ptr1 is a pointer to non-const chars, but your program will still invoke undefined behavior (and in practice, most likely crash, since string literals are placed in readonly memory regions).

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

    You are pointing to the same area in memory, but not qualifying it as const as well, which the argument is.

    You then allow the function body to modify that part of memory which is labelled const.

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