ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
void modify(int *arr[], int n) {
for (int i = 0; i < n; i++) *arr[i] += 100;
}
int main(void) {
int a = 1, b = 2, c = 3;
int *ptrs[3] = {&a, &b, &c};
modify(ptrs, 3);
printf("%d %d %d", a, b, c);
}- A. Compilation error: int *arr[] is not a valid parameter type
- B. 1 2 3
- C. 101 102 103
- D. 100 100 100
Correct answer: C. 101 102 103
ptrs is an array of pointers to distinct int variables, decaying to int** when passed. modify dereferences each element (*arr[i]) to reach the pointed-to int and adds 100 to it in place, mutating a, b and c through their addresses directly: 101 102 103.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int nums[] = {1, 2, 3, 4, 5}; int p = nums; for (int i = 0; i < 5; i++) { printf("%d ", p); p…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5; int p = &a; void vp = p; int p2 = (int )vp; p2 = 99; printf("%d %d", a, p);
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = a; int q = &a[2]; ptrdifft diff = q - p; printf("%td %d", diff…
- Assume a 64-bit LP64 ABI. What is printed? c int rows = 2, cols = 3; int matrix = malloc(rows sizeof(int )); for (int i = 0; i < rows…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; p += 5; p = p; p = 2; printf("%d", a);
- Assume a 64-bit LP64 ABI. What is printed? c int a[2][2] = {{1, 2}, {3, 4}}; int p = a[0]; int q = a[1]; printf("%ld", q - p);