ProgrammingHard

A function returns a pointer to a dynamically allocated copy of its argument string. Which signature is the most correct for the caller's use?

  1. A. char *dup(const char *s) that returns NULL on allocation failure and expects the caller to free the result
  2. B. char dup(char *s) that returns the first character
  3. C. void dup(char *s) that copies into a global buffer of 10 bytes
  4. D. char *dup(const char *s) that frees s before returning

Correct answer: A. char *dup(const char *s) that returns NULL on allocation failure and expects the caller to free the result

The copy must be freed by the caller, and the result must be checked for NULL. The const parameter documents that s is not changed.

More C Programming questions

Exams that ask this topic