Open In App

What’s difference between “array” and “&array” for “int array[5]” ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out!

To check this, the very first thing that comes to mind is the following program.

C++




// C++ Program to check whether 'array' and '&array'
// prints the same address
 
#include <iostream>
 
int main()
{
   int array[5];
   
   std::cout << "array = " << array << " : &array = " << &array;
 
   return 0;
}
 
// This code is contributed by sarajadhav12052009


C




#include <stdio.h>
int main()
{
   int array[5];
 
   /* If %p is new to you, you can use %d as well */
   printf("array = %p : &array = %p\n", array, &array);
 
   return 0;
}


Output

array = 0x7fffce606b90 : &array = 0x7fffce606b90

So you got same address for both “array” and “&array”. Again, you are tempted to think that both are same. Well, they are not! How come a variable and its & (i.e. address-of) be same. It doesn’t look logical but we saw that both “array” and “&array” are printing same address. May be it’s too soon to conclude. The crux of this post is that even though they both are resulting in same address but they are different types of addresses. And this is the difference between “array” and “&array”.

And just to show this difference, I would suggest to take a look at the following program.

C++




// C++ Program to check that adding 1 to both 'array' and '&array'
// prints the same address or not
 
#include <iostream>
using namespace std;
 
int main()
{
   int array[5];
  
   cout << "array = " << array << " : &array = " << &array << endl;
 
   cout << "array + 1 = " << array + 1 << " : &array + 1 = " << &array + 1;
   
   return 0;
}
 
// This code is contributed by sarajadhav12052009


C




#include <stdio.h>
 
int main()
{
   int array[5];
 
   /* If %p is new to you, you can use %d as well */
   printf("array = %p : &array = %p\n", array, &array);
 
   printf("array + 1 = %p : &array + 1 = %p", array + 1, &array + 1);
 
   return 0;
}


Output

array = 0x7ffc6ab5daa0 : &array = 0x7ffc6ab5daa0
array + 1 = 0x7ffc6ab5daa4 : &array + 1 = 0x7ffc6ab5dab4

With pointer arithmetic, we know what happens when we add an integer to a pointer. So can you guess the output of the above program without running it? Shouldn’t “array+1” and “&array+1” point to same address. Well you might be surprised 🙂

Basically, “array” is a “pointer to the first element of array” but “&array” is a “pointer to whole array of 5 int”. Since “array” is pointer to int, addition of 1 resulted in an address with increment of 4 (assuming int size in your machine is 4 bytes). Since “&array” is pointer to array of 5 ints, addition of 1 resulted in an address with increment of 4 x 5 = 20 = 0x14. Now you see why these two seemingly similar pointers are different at core level. This logic can be extended to multidimensional arrays as well. Suppose double twoDarray[5][4] is a 2D array. Here, “twoDarray” is a pointer to array of 4 int but “&twoDarray” is pointer to array of 5 rows arrays of 4 int”. If this sounds cryptic, you can always have a small program to print these after adding 1. We hope that we could clarify that any array name itself is a pointer to the first element but & (i.e. address-of) for the array name is a pointer to the whole array itself.



Last Updated : 02 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads