ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
void swap(int **a, int **b) {
    int *t = *a;
    *a = *b;
    *b = t;
}
int main(void) {
    int x = 1, y = 2;
    int *p = &x, *q = &y;
    swap(&p, &q);
    printf("%d %d", *p, *q);
}
  1. A. 2 1
  2. B. 1 2
  3. C. 2 2
  4. D. 1 1

Correct answer: A. 2 1

swap exchanges the pointers themselves, so p now points at y and q at x. Dereferencing gives 2 and then 1.

More C Programming questions

Exams that ask this topic