ProgrammingVery hardProduct company OA
What is printed? Assume a 64-bit LP64 ABI (4-byte int) and that the matrix is stored contiguously.
What is printed? Assume a 64-bit LP64 ABI (4-byte int) and that the matrix is stored contiguously.
int a[2][3] = {{1,2,3},{4,5,6}};
int *p = (int*)a;
printf("%d %d %d", *(p+4), *(*(a+1)+2), *(a[0]+4));- A. 5 6 5
- B. 5 6 4
- C. 4 6 5
- D. 5 5 5
Correct answer: A. 5 6 5
Flattening the 2x3 matrix, p+4 lands on the 5th int, which is a[1][1]=5. *(a+1)+2 is &a[1][2], value 6. a[0]+4 walks past row 0's bound into row 1's storage (contiguous, though technically out of the row's own bounds) landing on a[1][1]=5 again.
More C Programming questions
- What is printed? c int p = malloc(3 sizeof(int)); for (int i = 0; i < 3; i++) p[i] = malloc(3 sizeof(int)); for (int i = 0; i < 3; i++)…
- What is printed? c int r0[] = {1,2,3}; int r1[] = {4,5}; int r2[] = {6,7,8,9}; int jag[3] = {r0, r1, r2}; int lens[3] = {3,2,4}; int sum =…
- What is printed? c int a[4] = {10,20,30,40}; int arr[4]; for (int i = 0; i < 4; i++) arr[i] = &a[i]; int pp = arr; pp += 2; printf("%d…
- Which declaration correctly declares p as an array of 10 pointers to functions taking no arguments and returning a pointer to int?
- What is printed? c char words[] = {"bat", "cat", "rat", "mat"}; char p = words + 1; char q = words + 3; printf("%ld", (long)(q - p));
- What is printed? (This program has no undefined behavior.) c char fmt(int n) { static char buf[16]; sprintf(buf, "%d", n); return buf; }…