Possible Duplicate:
Modifying C string constants?
What is the difference between char *str when using malloc or not?
int i;
char *s = (char*)malloc(sizeof(char)*27);
for(i=0;i<26;i++)
s[i]='a'+i;
s[26]='\0';
printf("%s\n",s);
reverse(s);
printf("%s\n",s);
where reverse() is
void reverse(char *str)
{
int i,j=strlen(str)-1;
char tmp;
for(i=0;i<j;i++,j--)
{
tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
This works fine but when using
char *t = "new string";
printf("%s\n",t);
reverse(t);
printf("%s\n",t);
I get a segfault and the debugger says it is in strlen in reverse. Changing char *t to char t[] works fine. What gives?