Possible Duplicate:
Is it possible to modify a string of char in C?
#include <stdio.h>
void reverseStr(char *str);
main()
{
reverseStr("abcdef");
}
void reverseStr(char *str) {
char *tmp = str;
char curr;
while (*tmp != '\0') {
tmp++;
}
tmp--;
while (tmp > str) {
curr = *str;
*str = *tmp;
*tmp = curr;
str++;
tmp--;
}
}
When I run it, I get:
/usr/bin/runit/srun_c: line 12: 2809 Segmentation fault /tmp/run_c_executable
What on earth is going on? I'm practicing for an interview, I'm rusty in my C and wanted to practice something easy but can't for the life of me figure this out.
I've noticed the seg fault disappears when I comment out the *str = *tmp;
line, and I don't see why that should cause a seg fault.
Help appreciated.