top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between array_name and &array_name in C?

+3 votes
1,666 views
What is the difference between array_name and &array_name in C?
posted Nov 14, 2013 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

Let me explain this with an example.

int array_name [ ] = { 1, 2, 3 } ;

1> The name of the array (array_name) always gives the address of first element which is also equal to the base address of the array (array_name) so it acts as pointer to first element.
2> But when u want to access &array_name then it gives the address of the array which is the base address of the array But the type is different.Here the type is address of the array means pointer to the array.

3 >These differences u can proof by printing this

printf("Size = %d \t", sizeof(&array_name));    //  Output = 4 as it is a pointer to the array
printf("Size = %d\n", sizeof(array_name));       // Output = 12 it takes it takes base address of array name and calculates its size 

4> U also print this to make it more clear

int main () {
int array_name [ ] = { 1, 2, 3 } ;
printf( "array_name = %u \t &array_name = %u \t array_name + 1 =%u \t &array_name + 1%u \n ",  array_name , &array_name , (array_name + 1) , &array_name+1 );
return 0; }

The O/p will be

array_name = **********     &array_name = **********  
array_name +1 = **********  &array_name +1 = **********

Suppose base address of array name is **********
so both array_name and &array_name gives same
But array_name + 1 gives the address of 2nd element.
and &array_name + 1 gives the address evaluated after adding that many no of elements size.

&array_name + 1 = array_base address + ( No of elements * size of the element )

So both are Different.

answer Nov 16, 2013 by Sachidananda Sahu
...