ProgrammingHardIBM · Deloitte pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int rows = 2, cols = 3;
int **matrix = malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
matrix[i] = malloc(cols * sizeof(int));
for (int j = 0; j < cols; j++) matrix[i][j] = i * cols + j;
}
printf("%d %d %d", matrix[0][0], matrix[1][2], matrix[0][2]);- A. 0 5 2
- B. 0 4 2
- C. 0 5 3
- D. 1 5 2
Correct answer: A. 0 5 2
This builds a genuine dynamic 2D array as an array of row pointers, each row separately malloc'd. matrix[i][j] = i*cols+j: matrix[0][0]=0*3+0=0; matrix[1][2]=1*3+2=5; matrix[0][2]=0*3+2=2. Output: 0 5 2.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int x = 1; int p = &x; int pp = &p; int ppp = &pp; printf("%d", ppp == &pp && ppp == pp &&…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {10, 20, 30}; int b[3] = {10, 20, 30}; printf("%d %d", a == b, a == b);
- Assume a 64-bit LP64 ABI. What is printed? c int globalArr[5] = {1, 2, 3, 4, 5}; void resetArr(void) { for (int i = 0; i < 5; i++)…
- What is printed? c int main(void) { char c = 200; printf("%d", c); }
- What is printed? c int main(void) { int a = 10; int p = &a; int pp = &p; printf("%d %d %d", a, p, pp); a = 20; printf(" %d %d %d", a, p…
- What is printed? c enum Color { RED, GREEN, BLUE }; enum Color c = GREEN; printf("%d", c); enum Status { OK = 1, FAIL = 5, PENDING }…