title: Segmentation fault (core dumped) due to fgets - I think
body: <p>but I keep getting this error when I run this program. I think it's because of the fgets function. I tried initializing the input variable to NULL to see if that'll help, but it didn't. I also have a hunch that I might need to malloc to solve the problem. But your help is highly appreciated.</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      char* input = NULL;
  
      // ensure one and only one command line argument
      if (argc != 2)
      {
          printf("Usage: %s [name of document]\n", argv[0]);
          return 1;
      }
  
      // open a new document for writing
      FILE* fp = fopen(argv[1], "w");
  
      // check for successful open
      if(fp == NULL)
      {
          printf("Could not create %s\n", argv[1]);
          return 2;
      }
  
      // get text from user and save to file
      while(true)
      {
          // get text from user
          printf("Enter a new line of text (or \"quit\"):\n");
          fgets(input, 50, stdin);
  
          // if user wants to quit
          if (input != NULL &amp;&amp; strcmp(input, "quit") == 0)
          {
              free(input);
              break;
          }
          // if user wants to enter text
          else if (input != NULL)
          {
              fputs(input, fp);
              fputs("\n", fp);
              printf("CHA-CHING!\n\n");
              free(input);
          }
      }
  
      // close the file and end successfuly
      fclose(fp);
      return 0;
  }
  </code></pre>