#include<stdio.h>
void main()
{
char d;
char *r="Helloo";
printf("%s\n",r);
d=*(r+1);
printf("%c",d);
*(r+0)=d;
printf("%s\n",r);
}
this was working fine when i stored the string in a character array but why doesn't it work now
#include<stdio.h>
void main()
{
char d;
char *r="Helloo";
printf("%s\n",r);
d=*(r+1);
printf("%c",d);
*(r+0)=d;
printf("%s\n",r);
}
this was working fine when i stored the string in a character array but why doesn't it work now
char *r="Helloo";
You assigned pointer r
to a string literal. String literals should be treated as immutable. Any attempt to modify one leads to undefined behavior (N1570, Section 6.4.5/7).
With
char r[]="Helloo";
You have the string stored in an array that you can modify so it works as expected.