title: How to fix 'Segmentation Fault (core dumped)' error
body: <p>I am just starting to learn some C so I wrote a small program to exercise on using char arrays. 
  It takes in a string from <code>stdin</code> and prints it after removing trailing whitespaces and empty lines (if any of you have the second edition of 'The C programming language' it's problem 1-18). 
  After hitting the EOF key to end input, however, the program exits with error: 'Segmentation fault (core dumped)'.</p>
  
  <p>I've already compiled with <code>Wall</code>, <code>Wextra</code>, <code>Werror</code> and <code>g</code> but no errors are displayed at compile time (using gcc 9.1.1 on Fedora 30).
  I also ran the program through <code>gdb</code> and discovered that the line that causes the fault is this line: </p>
  
  <pre class="lang-c prettyprint-override"><code>for (; (c = input[start + i]) != '\n' || c != '\0'; ++i)
  </code></pre>
  
  <p>I'm pasting the whole file since it's a short program and everything could be the cause of the error.</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  #define MAX 100
  
  int Read(char input[], int max);
  void ProcessInput(char input[], char output[], int length);
  int GetToEndLine(char input[], char outLine[], int startIndex);
  
  int main(void){
      char input[MAX];
      char output[MAX];
      int length;
      printf("W: Max output is of %d chars.\n", MAX - 1);
      length = Read(input, MAX);
      ProcessInput(input, output, length);
      printf("\nCleaned input:\n---\n%s\n---\n", output);
      return 0;
  }
  
  int Read(char input[], int max){
      int i, c;
      for (i = 0; (c = getchar()) != EOF &amp;&amp; i &lt; max - 1; ++i)
          input[i] = c;
      input[i] = '\0';
      return i;
  }
  
  void ProcessInput(char input[], char output[], int length){
      int mainIndex = 0,
          new_mainIndex = 0,
          outputIndex = 0;
      char line[MAX];
      while ((length - mainIndex) &gt; 0){
          new_mainIndex = GetToEndLine(input, line, mainIndex);
          if (new_mainIndex == mainIndex){
              ++mainIndex;
              continue;
          }
          for (int j = new_mainIndex - mainIndex; 
                  line[j] == ' ' || line[j] == '\t' || line[j] != '\n'; --j)
              line[j] = '\0';
          for(int j = 0; line[j] != '\0'; ++j, ++outputIndex)
              output[outputIndex] = line[j];
          output[outputIndex] = '\n';
          ++outputIndex;
          mainIndex = new_mainIndex + 1;
      }
  }
  
  int GetToEndLine(char input[], char line[], int start){
      int c, 
          i = 0;
      for (; (c = input[start + i]) != '\n' || c != '\0'; ++i)
          line[i] = c;
      line[i] = c;
      return start + i;
  }
  </code></pre>
  
  <p>The full output of a successful test run should be:</p>
  
  <pre><code>W: Max output is of 99 chars.
  asdf   
  
  asdf
  Cleaned Input:
  ---
  asdf
  asdf
  ---
  </code></pre>
  
  <p>Instead of that I get:</p>
  
  <pre><code>W: Max output is of 99 chars.
  asdf   
  
  asdfSegmentation fault(core dumped)
  </code></pre>
  
  <p>Can somebody help me debug this program?</p>