What is the output of the following C code?

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.

#include <stdio.h>
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);
}

explain plz.

8Comments
Parimal Andhalkar @parimal_andhalkar
16 Jan 2016 11:49 am

o/p = 2036,2036,2036

 

Ankur Jain @ankur
16 Jan 2016 11:45 am

x+3 =>  x[3][0]

*(x+3) =>  x[3][0]

*(x+2)+3 => x[2][0]+3 =>  x[3][0]

if address of x[0][0] is 2000 thn address of x[3][0] will be 2036 as unsigned int is of 4B.

Vishnu Vardhan @vishnuvardhan
18 Jan 2016 07:52 pm

  Here x is an array of 4 elements in which each element is a single dimension array of 3 elements.

  x+3 refers to the starting address of 4th element (0, 1, 2, 3rd).

 Each element of the x is the size of 12 bytes( integer size is 4 bytes and 3 elements are there in each single dimension array).

x + 3 refers to 2000 + 12 * 3 = 2036,  the address of x[3][0].  x[3][0] refers to the actual value of the element.

The other two also represent the the address of x[3][0] in different format.

Tinu Dahiya @tinudahiya
18 Jan 2016 09:34 pm

size of int is 2 byte as I studied. Can you plz describe it with the memory diagram?

Ankur Jain @ankur
18 Jan 2016 09:58 pm

@ Tinu: The size of an int is compiler dependent and/or implementation dependent. In general it is 16 bit (2 bytes) on 16-bit platforms, 32 bit (4 bytes) on 32-bit platforms and 64 bit (8 bytes) on 64-bit platforms. And some time 64 bit platforms supports 32 bit integer.

Tinu Dahiya @tinudahiya
19 Jan 2016 02:03 am

@ankurjain then how to decide how many bits have to take to solve ques?? 

Vishnu Vardhan @vishnuvardhan
19 Jan 2016 06:35 pm

 an integer takes 4 bytes in 32 and 64 systems. long integer takes 8 bytes in 64 bit systems.

 Depending on the system you use, the address value may change. But the procedure for calculation of 

 address is same as explained above.

 
 

Tinu Dahiya @tinudahiya
19 Jan 2016 06:40 pm

thanks sir.

Pages