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?
Yes
No
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 2 of 2.

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].

Safvan AK said:   9 years ago
But both arr and &arr gives the base address. I'm still confused.

Please help to find out.

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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.