I have a question:

#include <stdio.h>

char * chstrswithf(char * a)
{
while(*a!='\0')
{
if(*a=='s')
*a='f';
a++;
}
return a;
}


int main(void)
{
	char string[]="this is a string";
	chstrswithf(string);
	printf("%s \n",string);
	return 0;
}

the code above is ok for running.
but when I replace the char string[]="this is a string"; with char* string="this is a string";, the error occurs. What's the reason?

I have a question:

#include <stdio.h>

char * chstrswithf(char * a)
{
while(*a!='\0')
{
if(*a=='s')
*a='f';
a++;
}
return a;
}


int main(void)
{
	char string[]="this is a string";
	chstrswithf(string);
	printf("%s \n",string);
	return 0;
}

the code above is ok for running.
but when I replace the char string[]="this is a string"; with char* string="this is a string";, the error occurs. What's the reason?

char *str is read only

This question has already been answered. Start a new discussion instead.