# C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 6)
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 = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}
``````
65474, 65476
65480, 65496
65480, 65488
65474, 65488
Explanation:

Step 1: int a = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a 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

Discussion:
29 comments Page 1 of 3.

Dharmesh said:   11 months ago
For second part of the question &a+1 means (&a) +1 as precedence of & is higher than the precedence of the arithmetic operator, and &a means base address of the array.

After 1st part pointer is pointing to 2nd array's 1st element. In order to reach the base address ie. Address of 1st array 1 element we need to add a size of 8*2=16 bytes to the address of (a+1) address.

65480+16=65496.

Now we have reached till base address &a now in order to reach (&a) +1 we need to add base address+ 4*2 more i.e. 65496+8= 65104.
The difference between 1st part and 2nd part will be 32 bits.
Address of (&a) +1 - a = 32 bits.

Thanks!
(1)

Dipak said:   2 years ago
In this, the base address is 64472.

&a means the initial address of array i.e. 64472.
In this array, 12 elements & each contain 2 bytes so 12 * 2 = 24.
so &a+1= 64472 + 24 = 64496.
means it shift the address after the a.

Omkar said:   2 years ago
@Sairam.

As you said a+1 means a and a+2 means a.

So, if a we have to access then what we have to add in a
is a+4 means a.

Tejas B said:   2 years ago
Thanks for explaining @Sairam.

SAIRAM said:   2 years ago
@All.

Here, I will explain it clearly,

#include<stdio.h>
int main()
{
int a = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}

a    
 1 2 3 4 --> a = 1 a = 2 a = 3 a = 4
 4 3 2 1 --> a = 4 a = 3 a = 2 a = 1
 7 8 9 0 --> a = 7 a = 8 a = 9 a = 0

In general,
'a' contains base address which is the address of the first row first element in the array i.e., a,
&a is also refers to same base address a

a+1 points to the address of a, which is the second-row first element.
Similarly a+2 points to the address of a, which is the third-row first element.

Each element or integer occupies 2bytes.
a means we have 3 rows and 4 columns. I each row we have 4 elements.

Question:

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

this means address of a = 1 is 65472
this means address of a = 2 is 65474
this means address of a = 3 is 65476
this means address of a = 4 is 65478
this means address of a = 4 is 65480.
a+1 points the address of a, which is second row first element. so answer is 65480

&a represents the whole array but stores only the base address which is the starting element array a.

&a+1 points fourth row first element which means a
address of a is 65472 ------ 1st row 1st element
address of a is 65480 ------ 2nd row 1st element
address of a is 65488 ------ 3rd row 1st element
address of a is 65496 ------ 4th row 1st element.

Totally we have to cross 12 elements to reach &a+1 i.e., a.

Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Hence, begining address of 4th row first element is 65472 + 24 = 65496. So, &a+1 = 65496 which is also address of a.
(5)

Saha said:   2 years ago
@Neha.

Ravi said:   3 years ago
&a should be a pointer to an array of 3 pointers points to arrays of 4 ints, Am I correct?

Mohan said:   4 years ago
a+1 means 1th one-dimensional array of a.
&a+1 means next 2D array like a.

Mohan said:   4 years ago
a+1 means 1th one-dimensional array of a.
&a+1 means next 2D array like a.

Abhishek said:   5 years ago
Thanks for explaining @Neha.