#include
int main()
{
void reverse(char *s);
char *s = "abcdefg";
reverse(s);
return 0;
}
void reverse(char *s)
{
char *end = s;
char temp;
while(*end){
end++;
}
end--;
while(s < end)
{
temp = *s;
*s++ = *end;
*end-- = temp;
}
}
char *s = "abcdefg";
指向常量,不能这么写
修改了下你的程序
#include <stdio.h>
void reverse(char *s);
int main()
{
char source[] = "abcdefg";
char *s = new char[100];
strcpy(s, &source[0]);
reverse(s);
printf("%s", s);
return 0;
}
void reverse(char *s)
{
char *end = s;
char temp;
while(*end){
end++;
}
end--;
while(s < end)
{
temp = *s;
*s++ = *end;
*end-- = temp;
}
}
运行结果
gfedcba
告诉你了,那是常量。你在reverse中还在试图修改它的值。