ProgrammingVery hardProduct company OA

What is the behavior?

What is the behavior?
int arr[5] = {1,2,3,4,5};
int *p = arr + 5;
printf("%p", (void*)p);
printf("%d", *p);
  1. A. Both the pointer formation and the dereference are well-defined, printing a[5]
  2. B. Forming arr+5 (one-past-the-end) is well-defined, but dereferencing it with *p is undefined behavior
  3. C. Both lines are undefined behavior
  4. D. Only the printf of the pointer is undefined, the dereference is fine

Correct answer: B. Forming arr+5 (one-past-the-end) is well-defined, but dereferencing it with *p is undefined behavior

C explicitly permits forming a pointer to one-past-the-end of an array (useful for loop bounds) without it being UB, but actually dereferencing that pointer (reading through it) IS undefined behavior, since there is no object there.

More C Programming questions

Exams that ask this topic