Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing

# C Programming - Arrays - Discussion

@ : Home > C Programming > Arrays > Find Output of Program - Discussion

"When ambition ends, happiness begins."
- (Proverb)
6.

What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?

``````#include<stdio.h>

int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}
``````

 [A]. 65474, 65476 [B]. 65480, 65496 [C]. 65480, 65488 [D]. 65474, 65488

Explanation:

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.

Step 2: printf("%u, %u\n", a+1, &a+1);

The base address(also the address of the first element) of array is 65472.

For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480

Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496

Hence the output of the program is 65480, 65496

 Akshay said: (Fri, Dec 10, 2010 01:08:27 PM) The solution is not clear to me. Why 4 has been multiplied?. Can anyone help me out.

 Wikiok said: (Tue, Mar 8, 2011 05:43:23 AM) Each "int" occupies 2 bytes. So it has to be multiplied with 2.

 Lavanya said: (Wed, Mar 9, 2011 10:15:37 AM) Can anyone explain briefly.?

 Neha said: (Thu, Aug 25, 2011 10:37:58 PM) As we know s+2 of a two dimensional array is s[2][0] means 1st element of 3rd row, as then 1st row's 1st element is s[0][0] then its clear now s+1 is the 1st element of 2nd row. Now to reach 1st element of 2nd row there are 4integers (see the columm number) to cross,i.e 4*2bytes=8bytes will be added to the base address [i.e. s[0][0]'s address 65472] 65472+8 = 65480.

 Whiteperl said: (Tue, Sep 20, 2011 04:21:13 PM) @neha. Thanks but how 65496 came?

 Varsha said: (Mon, Dec 19, 2011 07:28:09 PM) But how we got the base address is 65472?

 Nandu said: (Mon, Jan 2, 2012 02:46:26 AM) @varsha: In quesion he mension that the array starts from 65472, So base address z 65472

 Onkar said: (Wed, Feb 1, 2012 03:39:47 PM) What will happen in a++ ? If we want to move to a[0][1] what will be the expression ? Please explain.

 Niyati said: (Sat, Feb 18, 2012 05:45:15 AM) What does &a+1 mean?How the result of this is 65496?

 San said: (Tue, Jul 3, 2012 01:10:58 AM) &a+1 is the address of the variable stored next to the array. Array requires "12 int * 2 bytes = 24 bytes". Hence, 65472 + 24 = 65496. So, &a+1 = 65496.

 Reena said: (Fri, Jul 20, 2012 08:23:41 PM) How we can refer a[0][1] ?

 Siddharth said: (Thu, Mar 28, 2013 09:59:09 AM) Here if I am not wrong than if I have to print the address of first 4 appearing in the array than will I use like &(a+3)(a).