Why am I getting; initializing 'char *' with an expression like 'const char *' discards qualifiers?

I cannot understand why I get this warning from clang myself:

 function_prototype_const_modifier.c:13:8: warning: initializing 'char *' with an expression of type 'const char *' discards qualifiers [-Wincompatible-pointer-types] char *ptr1 = source; ^ ~~~~~~ 1 warning generated. 

The code is very simple.

 #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 = source; char *ptr2 = destination; while(*ptr1 != '\0') { *ptr2++ = *ptr1++; } *ptr2 = '\0'; return destination; } 

any idea?

+4
source share
6 answers

source is const char * , a pointer to constant characters, so characters cannot be changed by dereferencing a pointer (for example, source[0] = 'A'; is a violation of the restriction).

However, the use of this char * parameter overrides this restriction; a simple char * assumes that the characters pointed to by the ptr1 pointer are not constant, and now you can write ptr1[0] = 'A'; freely ptr1[0] = 'A'; no compiler errors ("diagnostic message").

Consider what this means when you pass a string literal. Since the string literal is "readonly" (this is a const char [] ), trying to change its contents is undefined behavior. Therefore, if you call

 my_strcpy(destination, "Constant String"); 

but in the code for some reason you write

 ptr1[0] = 'A'; 

you will not receive a compiler diagnostic message because ptr1 is a pointer to non-const characters, but your program will still refer to undefined behavior (and in practice, most likely a failure, since string literals are placed in the reading area in memory).

+12
source

You just need to change:

 char *ptr1 = source; 

in

 const char *ptr1 = source; 
+4
source

Since the LHS type is char * and the RHS type is const char * .

The reason is as the error says:

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

The operator allows you to drop the const qualifier, and it allows you to change the pointed line through ptr1 and ptr2 , and therefore the compiler complains.

+1
source

You point to the same area in memory, but you do not qualify it as const , which is an argument.

Then you allow the body of the function to change the portion of memory labeled const .

+1
source

You assign a pointer to a constant character to a pointer to a char. By doing so, you risk changing the character (s) .

+1
source

In this case, what can we do.

Thanks for the clarity information for @ user529758.

Just 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
source

Source: https://habr.com/ru/post/1441205/


All Articles