ProgrammingHardIBM · Deloitte pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
void process(int *data, int len) {
for (int i = 0; i < len; i++) data[i] *= 2;
}
int main(void) {
int arr[4] = {1, 2, 3, 4};
process(arr, 4);
printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
}- A. 1 2 3 4
- B. 2 4 6 8
- C. Undefined behaviour: process receives a decayed pointer, not the original array
- D. Compile-time error: arr cannot be passed to a function expecting int*
Correct answer: B. 2 4 6 8
Passing arr decays it to a plain int* pointing at the same memory; process() receives this pointer and modifies the underlying array elements directly through it (no copy is made of the array's contents). After doubling each, arr becomes 2 4 6 8 — the caller sees the mutation because arrays are always effectively passed by reference to the underlying data in C.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int global = 10; int getPtr(void) { return &global; } int main(void) { int p = getPtr(); p =…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void move(struct Point p, int dx, int dy) { p->x += dx; p->y +=…
- Assume a 64-bit LP64 ABI. What is printed? c void modify(int x) { x = 100; } void modifyPtr(int x) { x = 100; } int main(void) { int a =…
- Assume a 64-bit LP64 ABI. What is printed? c int allocateArray(int n) { int arr = malloc(n sizeof(int)); for (int i = 0; i < n; i++)…
- Assume a 64-bit LP64 ABI. What is printed? c int compare(const void a, const void b) { return (const int )a - (const int )b; } int…
- Assume a 64-bit LP64 ABI. What is printed? c void fillWithPointer(int pp, int value) { pp = malloc(sizeof(int)); pp = value; } int…