I know that string in C is immutable
That's not true. The correct version is: modifying string literals in C are undefined behaviors.
In main()
, you defined the string as:
char p[] = "jkahsdkjs";
which is a non-literal character array, so you can modify it. But what you passed to foo
is "fgfgf"
, which is a string literal.
Change it to:
char str[] = "fgfgf";
foo(str);
would be fine.