ProgrammingVery hardProduct company OA

What is the behavior?

What is the behavior?
int a[3] = {1, 2, 3};
memcpy(a, a + 1, 2 * sizeof(int));
printf("%d %d %d", a[0], a[1], a[2]);
  1. A. 2 3 3, well-defined
  2. B. Undefined behavior: memcpy's source and destination regions overlap, which memcpy does not support (memmove is required)
  3. C. 1 2 3, well-defined since nothing actually changes
  4. D. Compilation error

Correct answer: B. Undefined behavior: memcpy's source and destination regions overlap, which memcpy does not support (memmove is required)

memcpy requires the source and destination memory regions to not overlap; here copying a[1..2] into a[0..1] overlaps by one element, which is undefined behavior — memmove exists precisely to handle this safely by copying through a temporary buffer or copying in the correct direction.

More C Programming questions

Exams that ask this topic