Tracing a two-pointer loop over an array

The following C fragment runs on a system where int is 4 bytes. After the loop ends, give the final element at index 0, the final element at index 3, the final element at index 4, and the value of the expression q - p as a whole number of elements.


C
int a[5] = {2, 4, 6, 8, 10};
int *p = a;
int *q = a + 4;
while (p < q) {
    int t = *p;
    *p = *q;
    *q = t * 2;
    p++;
    q--;
}
Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Pointers Questions: