What’s difference between “array” and “&array” for “int array[5]” ?
Last Updated :
02 Nov, 2022
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;
}
Outputarray = 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;
}
Outputarray = 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.
Similar Reads
Difference between pointer to an array and array of pointers Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whol
4 min read
What are the differences between array_map(), array_walk() and array_filter() methods in PHP ? array_map() Method: The array_map() is used to modify all elements in one or more arrays according to some user-defined condition in an easy manner. It basically sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function. Synta
5 min read
Difference between Arrays and Pointers The array and pointers are derived data types that have lots of differences and similarities. In some cases, we can even use pointers in place of an array, and arrays automatically get converted to pointers when passed to a function. So, it is necessary to know about the differences between arrays a
7 min read
Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
Difference between Structure and Array in C Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array AR
2 min read
Difference between Stack and Array Stack: A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the element inserted at the last is the first element to come out. The insertion of an element into a
3 min read
Difference between Array, Queue and Stack Array: An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of
3 min read
Difference Between one-dimensional and two-dimensional array Array is a data structure that is used to store variables that are of similar data types at contiguous locations. The main advantage of the array is random access and cache friendliness. There are mainly three types of the array: One Dimensional (1D) ArrayTwo Dimension (2D) ArrayMultidimensional Arr
3 min read
array::begin() and array::end() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array containe
3 min read