blackbirb
blackbirb

Reputation: 19

I can't understand this segmentation fault

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.

Upvotes: 1

Views: 121

Answers (2)

Tofu
Tofu

Reputation: 3613

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.

Upvotes: 2

Blaze
Blaze

Reputation: 16876

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';

Upvotes: 4

Related Questions