#include<stdio.h>
#include <stdlib.h>
#define MAXLINE 10000
void reverse(char *str);
int main()
{
int c, i;
char j;
char S[MAXLINE], *p1;
p1=S;
for(c=getchar(); c!=EOF; i++)
{
S[i]=c;
}
reverse(p1);
return 0;
}
void reverse(char *str)
{
if(*str)
{
reverse(str+1);
putchar(*str);
}
}
Please help me correct the segmentation fault.
blade71(107)% gcc -o reverse reverse_2.c
blade71(108)% reverse<reverse.in>reverse.out
Segmentation fault (core dumped)
blade71(109)%
Cat of files and octal dumps in hex:
Have you heard of palindromes?
In the garden of Eden, on meeting:
"Madam, I'm Adam."
Napoleon on his capability after his first defeat:
"Able was I ere I saw Elba."
And of course, on diet:
"Doc, note: I dissent! A fast never prevents a fatness. I diet on cod."
blade71(114)% od -x reverse.in
0000000 4861 7665 2079 6f75 2068 6561 7264 206f
0000020 6620 7061 6c69 6e64 726f 6d65 733f 0a0a
0000040 496e 2074 6865 2067 6172 6465 6e20 6f66
0000060 2045 6465 6e2c 206f 6e20 6d65 6574 696e
0000100 673a 0a0a 224d 6164 616d 2c20 4927 6d20
0000120 4164 616d 2e22 0a0a 4e61 706f 6c65 6f6e
0000140 206f 6e20 6869 7320 6361 7061 6269 6c69
0000160 7479 2061 6674 6572 2068 6973 2066 6972
0000200 7374 2064 6566 6561 743a 0a0a 2241 626c
0000220 6520 7761 7320 4920 6572 6520 4920 7361
0000240 7720 456c 6261 2e22 0a0a 416e 6420 6f66
0000260 2063 6f75 7273 652c 206f 6e20 6469 6574
0000300 3a0a 0a22 446f 632c 206e 6f74 653a 2049
0000320 2064 6973 7365 6e74 2120 4120 6661 7374
0000340 206e 6576 6572 2070 7265 7665 6e74 7320
0000360 6120 6661 746e 6573 732e 2049 2064 6965
0000400 7420 6f6e 2063 6f64 2e22 0a0a
0000414