The GATE Grind

GATE 2015 CS – Question 62

Programming and Data Structures · Programming in C · 2 marks · Multiple choice

What is the output of the following C code? Assume that the address of $x$ is 2000 (in decimal) and an integer requires four bytes of memory.

int main() {
    unsigned int x[4][3] =
        {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
    printf("%u, %u, %u", x + 3, *(x + 3), *(x + 2) + 3);
}
  1. 2036, 2036, 2036
  2. 2012, 4, 2204
  3. 2036, 10, 10
  4. 2012, 4, 6

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) 2036, 2036, 2036

Explanation

Each row of `x` has 3 integers, which is 12 bytes. `x + 3` points to row 3 at address $2000 + 3 \times 12 = 2036$. `*(x + 3)` is that row, which decays to the same address 2036. `*(x + 2)` is row 2 at address 2024, and adding 3 moves it by 3 integers, which is 12 bytes, to 2036. All three values are 2036.