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);
}- A. 1 2
- B. 2 2
- C. 2 1
- 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
- Assume a 64-bit LP64 ABI. What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; p++; p += 10; printf("%d %d…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 100; int p = &x; int pp = &p; int ppp = &pp; ppp = 200; printf("%d %d", x, pp);
- Assume a 64-bit LP64 ABI. What happens when this runs? c int create(void) { int x = 42; return &x; } int main(void) { int p = create()…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = a; printf("%d ", p++); printf("%d ", ++p); printf("%d", p);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5; int p1 = &a; int p2 = &a; printf("%d ", p1 == p2); printf("%d", p1 == p2);
- Assume a 64-bit LP64 ABI. What is printed? c int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int p = (int )a; for (int i = 0; i < 6; i++)…