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 1200 in memory?
#include<stdio.h>
int main()
{
int arr[]={2, 3, 4, 1, 6};
printf("%u, %u, %u\n", arr, &arr[0], &arr);
return 0;
}
Answer: Option
Explanation:
Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array and initialized.
Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here,
The base address of the array is 1200.
=> arr, &arr is pointing to the base address of the array arr.
=> &arr[0] is pointing to the address of the first element array arr. (ie. base address)
Hence the output of the program is 1200, 1200, 1200
Discussion:
8 comments Page 1 of 1.
Akanksha said:
8 years ago
Can any one explain it. How the o/p came as 1200?
(1)
Nakul said:
8 years ago
The int is of 4 bytes, then how no effect of int?
Megha Ashok Pansare said:
9 years ago
How to find out the base adress?
AMPOLU said:
1 decade ago
int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i=0; i<=4; i++)
printf("%d, ", a[i]);
return 0;
}
void change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
O/P: is changed at 4 only then.
Initially *(b+1) is 7.
After for loop it is changed to 14.
Memory for pointers stored as a base pointers.
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i=0; i<=4; i++)
printf("%d, ", a[i]);
return 0;
}
void change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
O/P: is changed at 4 only then.
Initially *(b+1) is 7.
After for loop it is changed to 14.
Memory for pointers stored as a base pointers.
Samneet said:
1 decade ago
arr gives base address of an array(first element of array).
&arr gives address of an array of ints.It is basically a pointer to whole array.
e.g.:
int arr[]={1,2,3,4,5};
let base address of array is 1000.Integer is supposed to be of 4 bytes.
then,
arr = 1000;
&arr = 1000;
&arr[0] = 1000;
Now the major difference.
arr+1 = 1000+1 = 1004(as int occupy 4 bytes).
&arr+1 = 1000+(no. of elements * size of datatype ) = 1000+4*5=1020.
Hope you got this.
&arr gives address of an array of ints.It is basically a pointer to whole array.
e.g.:
int arr[]={1,2,3,4,5};
let base address of array is 1000.Integer is supposed to be of 4 bytes.
then,
arr = 1000;
&arr = 1000;
&arr[0] = 1000;
Now the major difference.
arr+1 = 1000+1 = 1004(as int occupy 4 bytes).
&arr+1 = 1000+(no. of elements * size of datatype ) = 1000+4*5=1020.
Hope you got this.
(1)
Arup said:
1 decade ago
Array names gives base address of an array
& arr[0] gives also base address and &arr also same
& arr[0] gives also base address and &arr also same
Aryan said:
1 decade ago
&arr means "address of" arr i.e. 1200. As simple as that.
Balavignesh said:
1 decade ago
Arr, &arr is pointing to the base address of the array arr.
I can't understand the above line in explanation.
I can't understand the above line in explanation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers