ProgrammingHardTCS NQT · Infosys pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
void swap(int **a, int **b) {
    int *tmp = *a;
    *a = *b;
    *b = tmp;
}
int main(void) {
    int x = 1, y = 2;
    int *p = &x, *q = &y;
    swap(&p, &q);
    printf("%d %d", *p, *q);
}
  1. A. 1 2
  2. B. 2 2
  3. C. 2 1
  4. D. 1 1

Correct answer: C. 2 1

swap takes pointer-to-pointer parameters so it can modify the actual pointers p and q in main, not just local copies of them. It swaps what p and q themselves point at: after the call p points to y (value 2) and q points to x (value 1).

More C Programming questions

Exams that ask this topic