1

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.

5
  • just a note, I accidentally deleted the closing brace at the end of the reverse function, this is not my issue. Commented May 1, 2019 at 12:49
  • Note that even the second edition of K&R is very dated. Despite my respect for the authors, I recommend choosing a book better aligned with the current version of the language. Commented May 1, 2019 at 12:57
  • printf("%s\n", c); in main(), expects a char * but c is an int. Commented May 1, 2019 at 12:58
  • @blackbirb - you can edit the question and insert the missing brace. Commented May 1, 2019 at 13:09
  • You're comparing two integers. That doesn't involve any memory access that should cause a segmentation fault. How are you so sure that that's the line where the seg fault is occurring? If it's really occurring there, then something has probably corrupted your stack because code that compares two stack variables shouldn't be able to seg fault. Do the print statements just before that statement print the values you'd expect . If so then that really suggests that it isn't that comparison that's causing the seg fault. Commented May 2, 2019 at 4:18

2 Answers 2

4

The j == i; line is not causing the problem, it's not doing anything. The issue is here:

while ((c = reverse(line)) != EOF) { printf("%s\n", c); } 

You're trying to print a string, but you're giving it c, which is an int. Passing the wrong data type to printf is undefined behavior and that's what causes your segmentation fault. You probably wanted this instead:

printf("%s\n", line); 

Also note that you didn't null-terminate your string yet. In your reverse function, you need this:

printf("BEFORE REVERSING\n"); line[i] = '\0'; 
Sign up to request clarification or add additional context in comments.

Comments

2

The line j == i is completely irrelevant to your segmentation fault. The main issue for your segmentation fault is your printf("%s\n", c); statement. Because you're trying to print a string while passing an int as the argument. If you want to print the result of getchar, you can use the function putchar or as the alternative printf("%c"). Try this and it shouldn't crash.

#include <stdio.h> #define MAXLENGTH 1000 int reverse(char line[]); int main() { int c = 0; char line[MAXLENGTH]; while ((c = reverse(line)) != EOF) { putchar(c); } return 0; } int reverse(char line[]) { 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"); return c; } 

I removed the redundant j == i statement as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.