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[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}
Answer: Option
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
Discussion:
29 comments Page 1 of 3.
Dharmesh said:
2 years 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!
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:
3 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[3][4].
&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[3][4].
Omkar said:
3 years ago
@Sairam.
As you said a+1 means a[1][0] and a+2 means a[2][0].
So, if a[0][1] we have to access then what we have to add in a
is a+4 means a[0][1].
As you said a+1 means a[1][0] and a+2 means a[2][0].
So, if a[0][1] we have to access then what we have to add in a
is a+4 means a[0][1].
Tejas B said:
3 years ago
Thanks for explaining @Sairam.
SAIRAM said:
3 years ago
@All.
Here, I will explain it clearly,
#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 [0] [1] [2] [3]
[0] 1 2 3 4 --> a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 a[0][3] = 4
[1] 4 3 2 1 --> a[1][0] = 4 a[1][1] = 3 a[1][2] = 2 a[1][3] = 1
[2] 7 8 9 0 --> a[2][0] = 7 a[2][1] = 8 a[2][2] = 9 a[2][3] = 0
In general,
'a' contains base address which is the address of the first row first element in the array i.e., a[0][0],
&a is also refers to same base address a[0][0]
a+1 points to the address of a[1][0], which is the second-row first element.
Similarly a+2 points to the address of a[2][0], which is the third-row first element.
Each element or integer occupies 2bytes.
a[3][4] 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[0][0] = 1 is 65472
this means address of a[0][1] = 2 is 65474
this means address of a[0][2] = 3 is 65476
this means address of a[0][3] = 4 is 65478
this means address of a[1][0] = 4 is 65480.
a+1 points the address of a[1][0], 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[0][0].
&a+1 points fourth row first element which means a[3][0]
address of a[0][0] is 65472 ------ 1st row 1st element
address of a[0][1] is 65474
address of a[0][2] is 65476
address of a[0][3] is 65478
address of a[1][0] is 65480 ------ 2nd row 1st element
address of a[1][1] is 65482
address of a[1][2] is 65484
address of a[1][3] is 65486
address of a[2][0] is 65488 ------ 3rd row 1st element
address of a[2][1] is 65490
address of a[2][2] is 65492
address of a[2][3] is 65494
address of a[3][0] is 65496 ------ 4th row 1st element.
Totally we have to cross 12 elements to reach &a+1 i.e., a[3][0].
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[3][0].
Here, I will explain it clearly,
#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 [0] [1] [2] [3]
[0] 1 2 3 4 --> a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 a[0][3] = 4
[1] 4 3 2 1 --> a[1][0] = 4 a[1][1] = 3 a[1][2] = 2 a[1][3] = 1
[2] 7 8 9 0 --> a[2][0] = 7 a[2][1] = 8 a[2][2] = 9 a[2][3] = 0
In general,
'a' contains base address which is the address of the first row first element in the array i.e., a[0][0],
&a is also refers to same base address a[0][0]
a+1 points to the address of a[1][0], which is the second-row first element.
Similarly a+2 points to the address of a[2][0], which is the third-row first element.
Each element or integer occupies 2bytes.
a[3][4] 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[0][0] = 1 is 65472
this means address of a[0][1] = 2 is 65474
this means address of a[0][2] = 3 is 65476
this means address of a[0][3] = 4 is 65478
this means address of a[1][0] = 4 is 65480.
a+1 points the address of a[1][0], 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[0][0].
&a+1 points fourth row first element which means a[3][0]
address of a[0][0] is 65472 ------ 1st row 1st element
address of a[0][1] is 65474
address of a[0][2] is 65476
address of a[0][3] is 65478
address of a[1][0] is 65480 ------ 2nd row 1st element
address of a[1][1] is 65482
address of a[1][2] is 65484
address of a[1][3] is 65486
address of a[2][0] is 65488 ------ 3rd row 1st element
address of a[2][1] is 65490
address of a[2][2] is 65492
address of a[2][3] is 65494
address of a[3][0] is 65496 ------ 4th row 1st element.
Totally we have to cross 12 elements to reach &a+1 i.e., a[3][0].
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[3][0].
(9)
Saha said:
4 years ago
@Neha.
How 65496 came? Explain please.
How 65496 came? Explain please.
(1)
Ravi said:
5 years ago
&a should be a pointer to an array of 3 pointers points to arrays of 4 ints, Am I correct?
Mohan said:
6 years ago
a+1 means 1th one-dimensional array of a.
&a+1 means next 2D array like a.
&a+1 means next 2D array like a.
Mohan said:
6 years ago
a+1 means 1th one-dimensional array of a.
&a+1 means next 2D array like a.
&a+1 means next 2D array like a.
Abhishek said:
6 years ago
Thanks for explaining @Neha.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers