c - strcpy function with pointers to character array -
in code below result stack overflow. though null character there both strings, strcpy loop should terminate source string has null character. why stack overflow occurs??
#include <stdio.h> #include<strings.h> int main(void) { char *str="hello world"; char *str1="good morning"; strcpy(str,str1); printf("%s",str); return 0; }
the error isn't stack overflow, modifying string literal.
str
pointer points string literal "hello world"
, , modifying string literal undefined behavior.
change str
to:
char str[100] = "hello world";
Comments
Post a Comment