上下文
我正在学习C,我正在尝试使用指针来反转一个字符串. (我知道你可以使用数组;这更多是关于学习指针.)
问题
尝试运行下面的代码时,我会继续获得分段错误. GCC似乎不喜欢* end = * begin;线.这是为什么?
特别是因为我的代码与the non-evil C function already discussed in another question几乎相同
- #include <stdio.h>
- #include <string.h>
- void my_strrev(char* begin){
- char temp;
- char* end;
- end = begin + strlen(begin) - 1;
- while(end>begin){
- temp = *end;
- *end = *begin;
- *begin = temp;
- end--;
- begin++;
- }
- }
- main(){
- char *string = "foobar";
- my_strrev(string);
- printf("%s",string);
- }