Ok, I'm learning C, and I'm trying to reverse a string in place using pointers. (I know you can use an array, this is more about learning about pointers.)
I keep getting segfaults when trying to run the code below. Gcc seems not to like the *end = *begin;
line. Why is that?
especially since my code is nearly identical to the non-evil c fn already discussed
#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);
}