C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 9)
9.
What will be the output of the program if the array begins at address 65486?
#include<stdio.h>
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u\n", arr, &arr);
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, &arr); Here,
The base address of the array is 65486.
=> arr, &arr is pointing to the base address of the array arr.
Hence the output of the program is 65486, 65486
Discussion:
17 comments Page 1 of 2.
Kajal said:
5 years ago
%u range is 0 to 65535 where int is 2B so how it can handle 65486?
Please explain.
Please explain.
Thinakaran said:
6 years ago
int arr[5] = {1, 2, 3, 4, 5};
printf("Address of arr is %p\n", (void*)arr);
printf("Address of &arr is %p\n", (void*)&arr);
printf("Address of arr + 1 is %p\n", (void*)(arr + 1));
printf("Address of &arr + 1 is %p\n", (void*)(&arr + 1));
and the output:
Address of arr is 0x7fff57266870.
Address of &arr is 0x7fff57266870.
Address of arr + 1 is 0x7fff57266874.
Address of &arr + 1 is 0x7fff57266884.
We find that:
(arr + 1) points to 874 which is 4 bytes away from arr, which points to 870 (I have removed the higher order bits of the address for brevity). An int on my machine takes up 4 bytes, so (arr + 1) points to the second element of the array.
(&arr + 1) points to 884 which is 20 bytes away from arr (points to 870).
(884 - 870 = 14 in hex = 20 in decimal).
Taking the size of int into consideration, (&arr + 1) is 5 int-sizes away from the beginning of the array. 5 also happens to be the size of the array. So, (&arr + 1) points to the memory address after the end of the array.
Why is (arr + 1) and (&arr + 1) different though arr and &arr point to the same location?
The answer - While (arr + 1) and (&arr + 1) have the values, they are different types.
arr is of the type int *, where as &arr is of the type int (*)[size].
So, &arr points to the entire array whereas arr points to the first element of the array.
printf("Address of arr is %p\n", (void*)arr);
printf("Address of &arr is %p\n", (void*)&arr);
printf("Address of arr + 1 is %p\n", (void*)(arr + 1));
printf("Address of &arr + 1 is %p\n", (void*)(&arr + 1));
and the output:
Address of arr is 0x7fff57266870.
Address of &arr is 0x7fff57266870.
Address of arr + 1 is 0x7fff57266874.
Address of &arr + 1 is 0x7fff57266884.
We find that:
(arr + 1) points to 874 which is 4 bytes away from arr, which points to 870 (I have removed the higher order bits of the address for brevity). An int on my machine takes up 4 bytes, so (arr + 1) points to the second element of the array.
(&arr + 1) points to 884 which is 20 bytes away from arr (points to 870).
(884 - 870 = 14 in hex = 20 in decimal).
Taking the size of int into consideration, (&arr + 1) is 5 int-sizes away from the beginning of the array. 5 also happens to be the size of the array. So, (&arr + 1) points to the memory address after the end of the array.
Why is (arr + 1) and (&arr + 1) different though arr and &arr point to the same location?
The answer - While (arr + 1) and (&arr + 1) have the values, they are different types.
arr is of the type int *, where as &arr is of the type int (*)[size].
So, &arr points to the entire array whereas arr points to the first element of the array.
Sanjana said:
7 years ago
We say that name of an array is a constant variable that stores the base address of the array. Now the variable must also have an address.
So, (&arrayname) must be different than (arrayname).
So, (&arrayname) must be different than (arrayname).
Ramya said:
8 years ago
@Puneet.
Why your multiplying size of (int) with 5?
Why your multiplying size of (int) with 5?
Balaji said:
8 years ago
How to find base address?
Manil Kumar said:
8 years ago
The base address of the array is 65486.
arr, &arr is pointing to the base address of the array arr., i,e same Address 65486.
arr, &arr is pointing to the base address of the array arr., i,e same Address 65486.
Anu said:
9 years ago
I can't understand arr+1,&arr+1. Please explain in detail.
Krutika said:
9 years ago
In simpler terms, the starting address of an array is equal to the starting address of the first element in an array.
Puneet said:
10 years ago
&arr+1 = Base address+5*size of (int).
Kundan singh said:
10 years ago
Arr gives the base address and here arr+1 means it gives the address of 2nd element. Here 1 denotes we are adding 2 byte to the base address.
And &arr+1 gives the address of next array of 5 integers means it will gives the address of last integers.
And &arr+1 gives the address of next array of 5 integers means it will gives the address of last integers.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers