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).
user529758
source share