c - Modifying the array element in called function -
i trying understanding passing of string called function , modifying elements of array inside called function.
void foo(char p[]){ p[0] = 'a'; printf("%s",p); } void main(){ char p[] = "jkahsdkjs"; p[0] = 'a'; printf("%s",p); foo("fgfgf"); }
above code returns exception. know string in c immutable, know there difference between modifying in main , modifying calling function. happens in case of other date types?
i know string in c immutable
that's not true. correct version is: modifying string literals in c undefined behaviors.
in main()
, defined string as:
char p[] = "jkahsdkjs";
which non-literal character array, can modify it. passed foo
"fgfgf"
, string literal.
change to:
char str[] = "fgfgf"; foo(str);
would fine.
Comments
Post a Comment