I call the function provided by Chris Conway (http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) from main (C code). When I run this program using cygwin, program crashes when it is in while loop (commented the lines where it breaks). Could you please explain what is going wrong here. Thanks
#include <stdio.h>
#include <string.h>
void strrev(char* z);
int main()
{
char *a;
printf("before reverse: %s\n", a);
strrev(a); // function provided by Chris Conway
printf("after reverse: %s\n", a);
return 0;
}
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; //crashes here (cygwin gives segmentation fault)
*end_ptr = temp; //for testing, if I comment out line above, it crashes here
str++;
end_ptr--;
}
}