String literals (your "Shiv") are not changed.
You assign the address of such a string literal to the pointer, then you try to change the contents of the string literal by dereferencing the value of the pointer. This is a big NO-NO.
Declare str as an array instead:
char str[] = "Shiv";
This creates str as an array of 5 characters and copies the characters "S", "h", "i", "v" and "\ 0" to str [0], str [1], ..., str [4] . The values โโin each str element are changed.
When I want to use a pointer to a string literal, I usually declare it const . That way, the compiler can help me by issuing a message when my code wants to change the contents of a string literal
const char *str = "Shiv";
Imagine you can do the same with integers.
int *ptr = &5; *ptr = 42; printf("5 + 1 is %d\n", *(&5) + 1);
Quote from the standard:
6.4.5 String literals
...
6 ... If the program tries to change such an array [string literal], the behavior is undefined.
source share