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 2 of 3.
Pihu said:
8 years ago
Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Why 1 is multiplied with 12*2?
Can anyone please explain?
Why 1 is multiplied with 12*2?
Can anyone please explain?
Siddharth said:
1 decade ago
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).
Onkar said:
1 decade ago
What will happen in a++ ?
If we want to move to a[0][1] what will be the expression ?
Please explain.
If we want to move to a[0][1] what will be the expression ?
Please explain.
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?
Nandu said:
1 decade ago
@varsha:
In quesion he mension that the array starts from 65472,
So base address z 65472
In quesion he mension that the array starts from 65472,
So base address z 65472
Akshay said:
1 decade ago
The solution is not clear to me.
Why 4 has been multiplied?.
Can anyone help me out.
Why 4 has been multiplied?.
Can anyone help me out.
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.
Wikiok said:
1 decade ago
Each "int" occupies 2 bytes. So it has to be multiplied with 2.
Vignesh said:
1 decade ago
Can anyone answer Reena's question since I'm having the same doubt.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers