title: Why add 1 when allocating space for strlen of an array of characters?
body: <p>I am currently working on a program which involves creating a template for an exam.
  In the function where I allow the user to add a question to the exam, I am required to ensure that I use only as much memory as is required to store it's data. I've managed to do so after a great deal of research into the differences between various input functions (getc, scanf, etc), and my program seems to be working but I am concerned about one thing. Here is the code for my function, I've placed a comment on the line in question:</p>
  
  <pre><code>int AddQuestion(){
  
  Question* newQ = NULL;
  char tempQuestion[500];
  char* newQuestion;
  
  if(exam.phead == NULL){
      exam.phead = (Question*)malloc(sizeof(Question));
  }
  else{
      newQ = (Question*)malloc(sizeof(Question));
      newQ-&gt;pNext = exam.phead;
      exam.phead = newQ;
  }
  
  while(getchar() != '\n');
  
  puts("Add a new question.\n"
       "Please enter the question text below:");
  fgets(tempQuestion, 500, stdin);
  
  newQuestion = (char*)malloc(strlen(tempQuestion) + 1); /*Here is where I get confused*/
  strcpy(newQuestion, tempQuestion);
  
  fputs(newQuestion, stdout);
  puts("Done!");
  
  return 0;
  }
  </code></pre>
  
  <p>What's confusing me is that I've tried running the same code but with small changes to test exactly what is going on behind the scenes. I tried removing the + 1 from my <code>malloc</code>, which I put there because <code>strlen</code> only counts up to but not including the terminating character and I assume that I want the terminating character included. That still ran without a hitch. So I tried running it but with - 1 instead under the impression that doing so would remove whatever is before the terminating character (newline character, correct?). Still, it displayed everything on separate lines.
  So now I'm somewhat baffled and doubting my knowledge of how character arrays work. Could anybody help clear up what's going on here, or perhaps provide me with a resource which explains this all in further detail?</p>