C Programming - Arrays - Discussion
Discussion Forum : Arrays - Yes / No Questions (Q.No. 4)
4.
Are the expressions arr and &arr same for an array of 10 integers?
Answer: Option
Explanation:
Both mean two different things. arr gives the address of the first int, whereas the &arr gives the address of array of ints.
Discussion:
15 comments Page 1 of 2.
Rupinderjit said:
1 decade ago
@Ram:You are right ,but the one thing here need to be consider is that, it also depends upon the dimensions of an array.you gave example of 1 D array.But problem is for @ D array.
In 2D array,an array in argument acts as a pointer(compiler dependent).so a(a+0) points to 1st row and a+1 points to second row and it's first element.
And &arr gives the address of array of ints(address of last element of an array which is equivalent to base address+size of type * number of elements).
In 2D array,an array in argument acts as a pointer(compiler dependent).so a(a+0) points to 1st row and a+1 points to second row and it's first element.
And &arr gives the address of array of ints(address of last element of an array which is equivalent to base address+size of type * number of elements).
Akshay said:
1 decade ago
Using the name of array directly always gives the base address of array i.e. address of first element in array. also using &arr will also give the address of first element so both the things are same. if someone wants to distinguish between addresses then he should provide position of the array element e.g. &arr[0] or &arr[5].
Ruku said:
7 years ago
@All.
arr and &arr points to the same location, they are different in type. arr has the type int *, where as &arr has the type int (*)[size]. &arr points to the entire array where as arr points to the first element of the array. This brings us to something useful - the length of the array.
arr and &arr points to the same location, they are different in type. arr has the type int *, where as &arr has the type int (*)[size]. &arr points to the entire array where as arr points to the first element of the array. This brings us to something useful - the length of the array.
Vallam said:
9 years ago
If we print the base address is same for both. While going to next element address it's different.
Let arr = &a[0] = 1000.
Arr+1 = 1004 as per GCC compiler. If it is the int type.
While & arr+1 means &arr holds 10 elements address. But shows as base address.
So & arr+1 = 1040.
Let arr = &a[0] = 1000.
Arr+1 = 1004 as per GCC compiler. If it is the int type.
While & arr+1 means &arr holds 10 elements address. But shows as base address.
So & arr+1 = 1040.
Maged said:
6 years ago
Basically, if you have an array of 5 elements, if you use (arr+1) it will point to the second element in the array, but if you use (&arr+1) it will point to the sixth element which is the first element out of the array.
Ram said:
1 decade ago
Try this
#include<stdio.h>
int main()
{
int arr[2];
printf("%d \n",arr);
printf("%d \n",(arr+1));
printf("%d \n",&arr);
printf("%d \n",(&arr+1));
}
#include<stdio.h>
int main()
{
int arr[2];
printf("%d \n",arr);
printf("%d \n",(arr+1));
printf("%d \n",&arr);
printf("%d \n",(&arr+1));
}
Teja said:
1 decade ago
arr and &arr are same.
both returns the base address of the arr at any time.
for better understanding execute ram's program.
but for 2D array arr,&arr[0] both returns the same address.
both returns the base address of the arr at any time.
for better understanding execute ram's program.
but for 2D array arr,&arr[0] both returns the same address.
Tony Joseoh said:
1 decade ago
Considering an Array &arr and arr returns the address of the first element of the array and the result goes same. And the answer is "YES" I believe.
Debendra Kumar kar said:
1 decade ago
Both value will be same only the scaling factor will change.
So out put will be same and the answer will be A as yes.
So out put will be same and the answer will be A as yes.
Safvan AK said:
9 years ago
But both arr and &arr gives the base address. I'm still confused.
Please help to find out.
Please help to find out.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers