Could someone point out the error in this
#include <stdio.h>
void modify (char*s,int x,int y)
{
s[x]=s[y];
}
main()
{
char* s = "random";
modify(s,1,2);
}
The program ends abruptly. I know this may be a very easy question but i am new to c.
Thanks !
It’s because it crashes during the assignment in
modify. The reason for that is that the pointer points to a constant string, one that can not be modified.If you want to modify the string, you can declare it as an array instead: