retagged by
27,797 views
110 votes
110 votes

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$
retagged by

6 Answers

Best answer
163 votes
163 votes

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.

edited by
34 votes
34 votes

This may help.

17 votes
17 votes
$\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.
edited by
7 votes
7 votes

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.


Answer:

Related questions

47 votes
47 votes
8 answers
3
go_editor asked Feb 14, 2015
15,861 views
Consider the following C program segment.# include <stdio.h int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = '0'; printf("%s", s1); }What will be printed by the pro...
42 votes
42 votes
2 answers
4
go_editor asked Sep 29, 2014
20,129 views
What does the following fragment of C program print?char c[] = "GATE2011"; char *p = c; printf("%s", p + p[3] - p );$\text{GATE2011}$$\text{E2011}$$2011$$011$