ProgrammingHardHCL · Tech Mahindra pattern

What happens?

What happens?
int arr[3] = {1, 2, 3};
int *p = arr;
p += 10;
*p = 99;
printf("done");
  1. A. Compilation error: pointer arithmetic cannot exceed array bounds
  2. B. Undefined behaviour: p is advanced 10 elements past a 3-element array and then dereferenced for a write, far outside any valid object
  3. C. It prints done and silently does nothing else
  4. D. It always crashes with a clear segmentation fault message

Correct answer: B. Undefined behaviour: p is advanced 10 elements past a 3-element array and then dereferenced for a write, far outside any valid object

Advancing a pointer more than one-past-the-end of its array is already undefined behaviour even before dereferencing; writing through it (*p = 99) compounds this into a genuinely undefined memory write, which might corrupt unrelated data, might crash, or might appear to 'work' — none of which is guaranteed or safe.

More C Programming questions

Exams that ask this topic