Because modifying string literal is undefined behavior - in your case leading to crash of your program.
In the second example, you are passing the string literal direclty and tried to make changes to it. Speaking in terms of char *newstr = strChanger("hi")
.
In fact char *str = "hi";
is basically making str
point to the string literal. More specifically string literal is an array which is converted into pointer to the first element and which is then assigned to str
. Then you tried to modify it - which is undefined behavior.
In the first case a copy of it is made which is modifiable and you can make changes to it which is then passed and it worked. You are declaring a char array and initializing it with the content of the string literal.
If you have POSIX defined "strdup"
then you can do this
char *newstr = strChanger(strdup("hi"));
But again then inside strChanger
you need to check the passed value - strdup
may return NULL
in case it fails to allocate memory and provide you with the copied string. At some point after using like this you will have to free the memory - free(newstr)
.
From standard 6.4.5p7 under string literal
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
From 6.7.9p14 under initialization
An array of character type may be initialized by a character string literal or UTF-8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.