What are the errors in this function from a Microsoft interview question?

Viewed 4142

I was asked this question in the MS written walkin-interview:

Find errors in the program below which is supposed to return a new string with \n appended to it.

char* AddnewlinetoString(char *s)
{
  char buffer[1024];
  strcpy(buffer,s);
  buffer[strlen(s)-1] = '\n';
  return buffer;
}

I'd tried to code on myself and was able to get it working by making buffer variable global and having buffer[strlen(s)] = '\n'. But did not know there were many other bugs in it.

12 Answers
Related