I think this question is an extension of this SO answer. Say I have the following code:
#include <stdio.h>
#include <string.h>
void func(char *str)
{
strcpy(str, "Test");
}
int main()
{
char testStr[20] = "Original";
func(testStr);
printf("%s\n", testStr); /* Prints "Test" followed by a new-line */
return 0;
}
By my understanding, shouldn't func
expect a pointer to a read-only literal as an argument? Whereas, what is being passed is a copy on the stack of a read-only literal.
Even though this yields correct results, is doing this 100% correct? Would it improve readability of code if func()
accepted char []
instead of char *
?