What will be output if you will compile and execute the following c code?…

What will be output if you will compile and execute the following c code?

#include<stdio.h>

int main(){

int a[2][4]={3,6,9,12,15,18,21,24};

printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]);

return 0;

}

Answer: B. 21 21 21Array layout: a[2][4] = {{3, 6, 9, 12}, {15, 18, 21, 24}}. *(a[1] + 2): a[1] refers to the second row (the array {15, 18, 21, 24}). Adding 2 moves to the…

  1. A.

    15 18 21

  2. B.

    21 21 21

  3. C.

    24 24 24

  4. D.

    None of above

Attempted by 119 students.

Show answer & explanation

Correct answer: B

Array layout: a[2][4] = {{3, 6, 9, 12}, {15, 18, 21, 24}}.

  • *(a[1] + 2): a[1] refers to the second row (the array {15, 18, 21, 24}). Adding 2 moves to the element at column index 2, so dereferencing yields a[1][2] = 21.

  • *(*(a + 1) + 2): a decays to a pointer to rows, so (a + 1) points to the second row. * (a + 1) yields that row (which then decays to a pointer to its first element); adding 2 and dereferencing gives a[1][2] = 21.

  • 2[1[a]]: Use the general rule x[y] = *(x + y). First, 1[a] is equivalent to a[1]. That decays to a pointer to the second row's first element; then 2[1[a]] is equivalent to *(2 + (pointer to row's first element)) which yields the element at index 2 of the second row, a[1][2] = 21.

Output: 21 21 21

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…