GATE 2015 COMPUTER SCIENCE & INFORMATION TECH. – CS – C Programming | Q. 62 (Shift – 1)

📌 GATE 2015 – Question 62
Question Type: MCQ
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);
}
Options:
(A) 2036, 2036, 2036      (B) 2012, 4, 2204
(C) 2036, 10, 10      (D) 2012, 4, 6
🔹 Understanding the 2-D Array
The declaration in the question is:
unsigned int x[4][3];
Therefore, x is a 2-D array with 4 rows and 3 columns. It contains a total of 4 × 3 = 12 elements.
The values given in the question are arranged as follows:
Index Column 0 Column 1 Column 2
Row 0 x[0][0] = 1 x[0][1] = 2 x[0][2] = 3
Row 1 x[1][0] = 4 x[1][1] = 5 x[1][2] = 6
Row 2 x[2][0] = 7 x[2][1] = 8 x[2][2] = 9
Row 3 x[3][0] = 10 x[3][1] = 11 x[3][2] = 12
📌 Important: In x[i][j], the first index i represents the row, while the second index j represents the column. Note that for computer 0 is also a number and numbering starts from 0. Humans generally start counting from 1. So, rwo and column numbering starts from 0.
For example: x[2][2] = 9 means row 2, column 2 contains the value 9.
🔹 Arrays and Pointers Are Closely Related

Before solving the 2-D array question, let us first understand the relationship between an array and a pointer using a simple 1-D array.

int a[3] = {10, 20, 30};

This array contains 3 elements. Remember that array indexing starts from 0.

Index Element Value Pointer Expression
0 a[0] 10 a or (a + 0) = &a[0]
1 a[1] 20 (a + 1) = &a[1]
2 a[2] 30 (a + 2) = &a[2]
📌 Key Idea: When the array name a is used in an expression, it represents a pointer to the first element, a[0]. &a[0] represents address of element a[0]. Here & is the ‘address of operator’. Like every operator returns a value, & returns address or pointer or memory location of an element that it is used with.

Therefore:

a + 0 → points to a[0] → value 10
a + 1 → points to a[1] → value 20
a + 2 → points to a[2] → value 30

To obtain the value stored at these locations, we use the dereference operator (*):

*(a + 0) = a[0] = 10
*(a + 1) = a[1] = 20
*(a + 2) = a[2] = 30
💡 Remember: a[0], a[1], and a[2] are the three elements of the array. The numbers inside the square brackets are called indices, and indexing starts from 0.
🔹 Pointer Representation of a 2-D Array

Now let us apply the same idea to the 2-D array used in this question:

unsigned int x[4][3]

This array has 4 rows and 3 columns. In a 2-D array, the array name x, when used in an expression, represents a pointer to the first row.

📌 Key Difference: In a 1-D array, pointer arithmetic moves from one element to the next. In this 2-D array, x + 1 moves from one row to the next row.
Row Array Row Pointer Expression Points To
Row 0 x[0] x or x + 0 Row 0
Row 1 x[1] x + 1 Row 1
Row 2 x[2] x + 2 Row 2
Row 3 x[3] x + 3 Row 3
The four rows of the array are:
x[0] = {1, 2, 3}
x[1] = {4, 5, 6}
x[2] = {7, 8, 9}
x[3] = {10, 11, 12}
💡 Therefore:
x → points to Row 0
x + 1 → points to Row 1
x + 2 → points to Row 2
x + 3 → points to Row 3
📌 Important: The expression x + 1 does not point to the next individual element such as x[0][1]. It points to the next complete row, because each element of x is itself a row containing 3 unsigned int elements.
🔹 Memory Addresses of the 2-D Array Elements

The question states that the address of x is 2000 (in decimal), and each unsigned int requires 4 bytes of memory.

📌 Important: The elements of a 2-D array are stored in contiguous memory locations, row by row. Therefore, the address increases by 4 bytes from one integer element to the next.
Element Address Address (Decimal)
&x[0][0] 2000
&x[0][1] 2004
&x[0][2] 2008
&x[1][0] 2012
&x[1][1] 2016
&x[1][2] 2020
&x[2][0] 2024
&x[2][1] 2028
&x[2][2] 2032
&x[3][0] 2036
&x[3][1] 2040
&x[3][2] 2044
💡 Observe the pattern:
2000 → 2004 → 2008 → 2012 → 2016 → … → 2044

Each successive unsigned int occupies the next 4 bytes of memory.
🔹 Finding the Value of (x + 3)

We have already seen that x is a 2-D array with 4 rows and 3 columns. In a 2-D array, pointer arithmetic with the array name x moves row by row.

x + 0 → Row 0
x + 1 → Row 1
x + 2 → Row 2
x + 3 → Row 3
📌 Therefore:
x + 3 points to Row 3 of the 2-D array.

Row 3 contains three elements:

Element Value Address
x[3][0] 10 2036
x[3][1] 11 2040
x[3][2] 12 2044

Since x + 3 points to the beginning of Row 3, it points to the address where Row 3 starts.

x + 3 = &x[3]
&x[3] = &x[3][0]
&x[3][0] = 2036
💡 Important Point:
Although Row 3 contains three elements at addresses 2036, 2040, and 2044, the expression x + 3 points to the start of Row 3. Therefore, its address is 2036.
📌 Conclusion:
x + 3 = 2036
Next: We will now understand what happens when we apply the * (dereference) operator to (x + 3).
🔹 Understanding *(x + 3)

We have already established that x + 3 points to Row 3 of the 2-D array.

x + 3 → points to Row 3
x + 3 = &x[3]
&x[3] = &x[3][0] = 2036
Now we apply the dereference operator (*):
*(x + 3) = x[3]

Row 3 contains three elements:

Element Value Address
x[3][0] 10 2036
x[3][1] 11 2040
x[3][2] 12 2044
📌 What does x[3] represent?
x[3] represents the entire fourth row: {10, 11, 12}.

But there is one more important point. Since x[3] is an array, when it is used in an expression, it is converted to a pointer to its first element.

x[3]
↓
&x[3][0]
↓
Address = 2036
💡 Therefore:

*(x + 3) = x[3]

x[3] is the fourth row {10, 11, 12}. When this array is used as a value in an expression, it refers to the address of its first element, x[3][0].

Therefore: *(x + 3) = 2036
📌 In one line:
x + 3 → Row 3 → *(x + 3) → x[3] → &x[3][0] → 2036
🔹 An Important Point About *(x + 3)

The expression we are evaluating in the question is:

*(x + 3)

We already know that:

x + 3 → points to Row 3
*(x + 3) → gives Row 3

Row 3 is:

x[3] = {10, 11, 12}
📌 What does “used in an expression” mean?

The array x[3] can be converted to a pointer to its first element when it is used in a context where a pointer is required. In that case:

x[3] → &x[3][0]
&x[3][0] = 2036
💡 Important:
We should not immediately say that *(x + 3) is 2036. The exact type and context of the expression must be considered.
Next: We will examine the type of x, x + 3, and *(x + 3) step by step. This will tell us exactly what happens in the printf() statement.
🔹 Understanding *(x + 2) + 3

Let us evaluate the expression step by step:

*(x + 2) + 3
Step 1: Find x + 2

In the 2-D array, x points to Row 0. Therefore:

x + 2 → Row 2

Row 2 contains: {7, 8, 9}
Step 2: Apply the * operator

*(x + 2) = x[2]

Therefore, *(x + 2) represents Row 2:
x[2] = {7, 8, 9}
Step 3: What happens when we add 3?

Here we have:

x[2] + 3

Since x[2] is an array, in this expression it is converted to a pointer to its first element:

x[2] → &x[2][0]
Element Value Address
x[2][0] 7 2024
x[2][1] 8 2028
x[2][2] 9 2032
Step 4: Move 3 integer positions

The pointer starts at &x[2][0], whose address is 2024.

Each unsigned int occupies 4 bytes. Therefore, moving 3 positions means:

3 × 4 = 12 bytes

Hence:

2024 + 12 = 2036
💡 Therefore:

*(x + 2) + 3
= x[2] + 3
= &x[2][0] + 3
= 2024 + (3 × 4)
= 2036
📌 Key point: The + 3 here is pointer arithmetic. It moves three unsigned int positions from the beginning of Row 2. It does not mean adding the number 3 to the value 7.
✅ Final Answer

From the above calculations:

x + 3 = 2036

*(x + 2) + 3 = 2036

Therefore, the required output of the given printf() expression is:

2036, 2036, 2036
🎯 Correct Option:

(A) 2036, 2036, 2036
0

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »