Skip to content
Official PYQ · 2015

GATE CS 2015 Set 1 Q35 — Arrays, Stacks and Queues

Explain with AI

What is the output of the following C code? Assume that the address of xx is 20002000 (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); 
}
Choose one option
View worked solution

Answer: A

To find the output of the printf statement, let's analyze the memory layout of the 2D array x.

Given:

  • Base address of x = 20002000
  • Size of an integer = 44 bytes
  • Array declaration: unsigned int x[4][3] (4 rows, 3 columns)

The size of each row of the array is: Row size=3×4 bytes=12 bytes\text{Row size} = 3 \times 4\text{ bytes} = 12\text{ bytes}


Step-by-Step Evaluation:

  1. First Expression: x + 3

    x is a pointer to an array of 3 integers (type: unsigned int (*)[3]). Performing pointer arithmetic on x scales by the size of a row (1212 bytes).

    Value=Base Address+3×Row size=2000+3×12=2036\text{Value} = \text{Base Address} + 3 \times \text{Row size} = 2000 + 3 \times 12 = 2036
  2. Second Expression: *(x + 3)

    Dereferencing x + 3 yields a pointer to the first element of the 4th row (type: unsigned int *). Numerically, the address of the row itself and the address of its first element are identical.

    Value=2036\text{Value} = 2036
  3. Third Expression: *(x + 2) + 3

    First, *(x + 2) points to the first element of the 3rd row (index 2):

    Address of ∗(x+2)=2000+2×12=2024\text{Address of } *(x + 2) = 2000 + 2 \times 12 = 2024

    Since *(x + 2) is of type unsigned int *, adding 33 to it scales by the size of an integer (44 bytes):

    Value=2024+3×4=2024+12=2036\text{Value} = 2024 + 3 \times 4 = 2024 + 12 = 2036

Thus, the output printed is 2036, 2036, 2036.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions