char* p="Hi, this is not going to work";
it does not allocate memory for you to write
String Literal is created, which results in an Undefined Behaviour every time the content changes.
use p as a buffer for your scanf to do something like char * p = malloc(sizeof(char) * 128); // 128 is an Example char * p = malloc(sizeof(char) * 128); // 128 is an Example
OR
you also can:
char p[]="Hi, this is not going to work";
I think this is what you really wanted to do.
Keep in mind that this can still be UB , because scanf() does not check if the location you are using is really writeable.
remember:
char * p is a string literal and should not be changed
char p[] = "..." allocates enough memory to store the string inside "..." and can be changed (its contents I mean).
Edit:
A good trick to avoid UB is
char * p = malloc(sizeof(char) * 128); scanf("%126s",s);
Muggen
source share