ProgrammingVery hardProduct company OA

What is printed?

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

Correct answer: D. 1 2 2 1 1 2

s1 swaps its own copies of the pointers and changes nothing for the caller. s2 receives the addresses of p and q and swaps the pointers themselves, so p now points to y and q to x: *p = 2, *q = 1. No int was swapped, so x and y are still 1 and 2.

More C Programming questions

Exams that ask this topic