Why is the below code working in Visual Studio ?
char* str1 = "This is a test string";
strcpy_s(str1, strlen(str1), "replacing content");
My understanding is str1 is just a char* pointing to a string literal, and not to an array of chars. And in strcpy_s() (or strcpy()), char bytes are copied from src to dst array.
In the above code, isn't strcpy() trying to overwrite a string literal ? If yes, then why is the code compiling ?
additional info
Not only is the code compiling, I can see the new string getting copied.
void stringCopy_demo() {
char* str1 = "1234567890"; //len = 10
printf("%s \n", str1);
strcpy_s(str1, strlen(str1), "content");
printf("%s \n", str1);
}
ouput
1234567890
content