ProgrammingVery hard

char src[] = "hey"; char dst[8]; memcpy(dst, src, strlen(src)); printf("%d", (int)strlen(dst)); What is the problem with this code?

  1. A. It prints 3 with no problem
  2. B. memcpy does not copy the terminator, so strlen(dst) reads uninitialised bytes, which is undefined
  3. C. It prints 0 because memcpy clears dst
  4. D. It fails to compile because memcpy needs three arguments

Correct answer: B. memcpy does not copy the terminator, so strlen(dst) reads uninitialised bytes, which is undefined

The terminator is not copied, so dst is not a valid string. Use memcpy with strlen(src) + 1 bytes, or strcpy.

More C Programming questions

Exams that ask this topic