-4

I'm trying to make K&R 2 exercise 1-20. Everything compiles though whenever I give it a input it gives me a segmentation fault (core dumped). I don't know what this means, whats causing it and how to fix it.

#include <stdio.h>

#define MAXLINE 1000
#define TAB 4

int main()
{
  int c, i, j;  /*len is the length of the line; i & j is for for loops*/

  char line[MAXLINE];             /*Line input*/

  while ((c=getchar())!=EOF){
    if (c == '\n'){
      printf("%s", line);
      }else{
        if (c == '\t'){
          for (j=0;j<TAB+1;++j){
            line[i+j] = ' ';
          }
        }else{
      line[i] = c;
      }
    }
  }
  return 0;
}

Jakob Sachs
  • 669
  • 4
  • 24
Lutpoint
  • 3
  • 1

1 Answers1

-2

Ok so you get this error segmentation fault when you are touching the memory which you shouldn't

for example #include <stdio.h>

int main(void)
{
    int a[2];
    a[0] = 1;
    a[1] = 5;

    for (int i = 10000000 ; i < 10000001; i++)
   {
    printf("%i",a[i]);
   }

}

enter code here