Possible Duplicate:
Why is this C code causing a segmentation fault?
My code:
void strrev(char *str) {
char temp, *end_ptr;
/* If str is NULL or empty, do nothing */
if( str == NULL || !(*str) )
return;
end_ptr = str + strlen(str) - 1;
/* Swap the chars */
while( end_ptr > str ) {
temp = *str;
*str = *end_ptr;
*end_ptr = temp;
str++;
end_ptr--;
}
}
void main() {
char temp;
char* x = "Hel2313lo123";
//temp = *x;
// strReverse(x);
strrev(x);
printf("\n%s", x);
}
And the function strrev() is in fact, copied straight from: How do you reverse a string in place in C or C++?
I get a segmentation fault whenever I try to run this. Why would that be happening?
THank you!