To preface: I am very new to both programming and C in general. I am going through K&R and I am trying to solve exercise 1-19.
I don't understand what is causing this segmentation fault in the line j == i;.
I've been trying various combinations of code to find out whats wrong.
I originally had a loop to try and reverse the character array, as per the exercise, but through troubleshooting I arrived at the j == i; line.
Nothing seems wrong with these two values, but trying to compare them gives me an error.
#include <stdio.h> #define MAXLENGTH 1000 int reverse(char line[]); main() { int c = 0; char line[MAXLENGTH]; while ((c = reverse(line)) != EOF) { printf("%s\n", c); } return 0; } int reverse(char line[]) { char r[MAXLENGTH]; int i, c, j, l; i = c = j = l = 0; printf("BEFORE GETARRAY\n"); while ((c = getchar()) != '\n' && c != EOF) { line[i] = c; i++; printf("i: %d\n", i); } l = i - 1; printf("i: %d\n", i); printf("l: %d\n", l); printf("j: %d\n", j); printf("BEFORE REVERSING\n"); j == i; return c; I would expect the comparison to conclude without error.
If you could describe not just what is wrong, but why, so that I can try improving, that would be wonderful.
printf("%s\n", c);inmain(), expects achar *butcis anint.