C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 11)
11.
What will be the output of the program if the array begins at 65486 and each integer occupies 2 bytes?
#include<stdio.h>
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u\n", arr+1, &arr+1);
return 0;
}
Answer: Option
Explanation:
Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.
Step 2: printf("%u, %u\n", arr+1, &arr+1);
Here, the base address(also the address of first element) of the array is 65486.
=> Here, arr is reference to arr has type "pointer to int". Therefore, arr+1 is pointing to second element of the array arr memory location. Hence 65486 + 2 bytes = 65488
=> Then, &arr is "pointer to array of 5 ints". Therefore, &arr+1 denotes "5 ints * 2 bytes * 1 = 10 bytes".
Hence, begining address 65486 + 10 = 65496. So, &arr+1 = 65496.
Hence the output of the program is 65488, 65496.
Discussion:
3 comments Page 1 of 1.
Magesh said:
2 decades ago
Points used to store the address.The address of arr+1
is(65488), So 5 number(int).then 5*2=10.
I have one question. What need for adding base address?
is(65488), So 5 number(int).then 5*2=10.
I have one question. What need for adding base address?
Mageshbabu said:
2 decades ago
How did &arr+1 is equal to total int*total bytes. I don't understand explain in the details.
Lavanya said:
2 decades ago
Please can any one explain again?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers