8.3k views

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$

recategorized | 8.3k views
+2
+3

Address of $x$ is $2000$.

$x$ being a $2 \ D$ array,

$x + 3 = x + 3 *$ sizeof its inner dimension

$= 2000 + 3 * 3 * 4$ (as inner dimension is $3$ integers of size $4$)

$= 2000 + 36 = 2036$.

$*(x+3)$ returns the value at address $2036$. But since $x$ is $2-D$ array, one $*$ will just return the $1 \ D$ array which is the starting address of it, which is $2036$ only.

$(x + 2) = 2000 + 2 * 3 * 4 = 2024$
$*(x + 2) + 3 = 2024 + 3 * 4 = 2036$ (The $*$ changes the data type from $2D$ to $1D$ and hence $+ 3$ will add $3*4$ and not $3 * 3 * 4)$

So, A.

by Veteran (430k points)
edited by
+17
    x + 3, 3 row skip so return address of starting of 4
rth = 2036 * (x + 3), 3 row skip so return address of 4rth row 1st column = 2036 * (x + 2) + 3 1st skip 2 row then skip 3 column i.e. address of 4rth row 1st elemnt

untill ** not used inside value not print  because it is two dimentional array.

+63

This can help to visualize some more.

+1
useful
0

explain this line plzz...

* (x + 2) + 3)
0
@Proton ,why addresses are 8 byte in 1st level ?!
+4

The array has 4 rows and 3 column.

Say x=2000

then what is the last address of the 1st row?

2000+3*4=2012 where 4 is size of each element(as it is integer) and 3 elements in each row.

2012 also starting address of 2nd row

So, x+1 address starts at 2012 and 2024 where 3rd row starts.

Similarly x+2 starts at 2024 and end at 2036 , where 4th row starts

So,

* (x + 2) + 3)

where *(x+2) means where 3rd row starts and +3 is from there we just need to add address of 3 elements

0
then even the second statement  *(x+3)

the * changes it to 1D array and it should be 2000 + 3 *4 = 2012 right?

0
got it :) sorry fa the silly doubt
0
Why 0 , 8 , 16 ,24 ?
0
How can we print the element of 2D array using same notation?
+1
In this case (x+3) should give 24.
0
@ANKITH

exactly
0
Sir how can i understand that i have to traverse 3 rows by ( x+3 )? Why cant i traverse 3 elements of a row by (x+3). Please help!

is it due to array representation as x[4][3] ?
0
how x+3 and *(x+3) is the same, not able to understand as x+3 return the address and the *(x+3) will return the value at the address
0

0
I can't understand the concept of (x+3) = *(x+3) .

How the 2d array is stored in computer memory??
0
According to this diagram  x gives 0,

x+3 gives 24,

*(x+3) gives 2036.

Please correct me if I am wrong.
0

@ankitrazzagmail.com   yes you r right

This may help.

by Junior (835 points)
+1
whyoption C is wrong? since it is asking *(x+3) which is the value at address 2036.Right ?
0
What if we replace all %u with %d
+2

%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative.

0
I said what will be the output if we replace %u by %d what will be the output answer?
0
This is a really good answer.

Thanks.
$\underline{\mathbf {Shortcut \;to\; answer\; this\; question\; very\; fast\; in\; the\; exam:}}$

Anything less than $\mathbf{2\;stars}$ in the $\mathrm{2-D}$ array is the address.

Now, look at the options.

Only the option $\mathbf A$ consists of values that are all addresses.

Other options have a mix of addresses and data values, that is they have both address and data values together. So only option $\mathbf A$ has values which are addresses.

$\therefore$ Blindly we can say that $\mathbf A$ is the correct answer.
by Boss (18.8k points)
edited by
0

0

@techbd123

+1
yes you can do like this.
0
Thanks.
+1

This line should be elaborated.

A twist: If the printf line was like below, the answer would be $2036,2036,2024$.

printf ("%u, %u, %u", x+3, *(x+3), *(x+1) + 3);

0
Yes but that again addresses only which proves the shortcut again.
0
Yeah. It works in this question. But what if all the options contained various confusing addresses?
+1
Yeah, that is obvious.

You have to use this trick with a caveat which I have mentioned in the answer.

The point is to save some precious time with such tricks and nothing more.
+1
well that's a savage method. AWESOME🤩. That's why I always emphasize "WRITERS ON GATE OVERFLOW ARE WAY BETTER THAN ANY OTHER WEBSITE /INSTITUTE "
0
So in general we can say, anything less than n * in an nd array is an address?
0

@commenter commenter

Yes. For eg: 2-d array

$a[i][j]$

$*(*(a+i)+j)$

Explanation supporting *(x+3) = 2036

• int A[m][n];
• We can think of A[0] as the address of row 0, A[1] as the address of row 1
• In general: A[i][j] = *(A[i] + j) = *(*(A+i)+j)
• Example: A[0][2] = *(A[0] + 2)

o Note that: A[0] = *A
• Hence, if A is a 2D int array, we can think of A as a pointer to a pointer to an integer. That is, int**

Hence *(x+3) would return 2036.
by Active (2.5k points)
0
Please explain how x+3 = *(x+3) ???
0
x is a 2D array, it decays to a 2D pointer. So x + 3 with one level of indirection is equivalent to *(x + 3).

Note: The terminology I've used here might or might not be standard.

For any n dimensional array, we need n asterisks$(*)$ in valid pointer code text to get the array elements.

         printf ("%u, %u, %u", x + 3, *(x + 3), *(x + 2) + 3);

Here, we've used max one asterisk in any argument, and the array is 2 dimensional. So no way we'll get the elements of the array as the output.

So, Options B, C and D are wrong. A must be the answer.

Now, why is A the answer?

x + 3

We know the array name stores the address of the first element of the array. Here, what does x store? 1? No.

The first element is {1,2,3}, ie, the first "sub array"

Hence, x+3 would point to {10,11,12}. It's easy to notice that is the address 2036.

*(x + 3)

As seen, (x+3) points to {10,11,12}. So, dereference it, you'll reach the address of the first element of it, ie 10.

Address of 10, is 2036, of course.

*(x + 2) + 3

Very similarly, (x+2) Would point to {7,8,9}. Dereference this "sub array" you'll now point to just 7. Add 3, you'll point to 10. So you'll point at the address 2036.

by Loyal (6.2k points)