If someone has defined an array such as “int array”, 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.
So you got same address for both “array” and “&array”. Again, you are tempted to think that both are same. Well, they aren’t 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.
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 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.
Please do Like/Tweet/G+1 if you find the above useful. Also, please do leave us comment for further clarification or info. We would love to help and learn 🙂
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between const int*, const int * const, and int const *
- Difference between "int main()" and "int main(void)" in C/C++?
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Difference between int (*p) and int* p?
- Difference between an Integer and int in Java with Examples
- C/C++ program to find the size of int, float, double and char
- Implicit return type int in C
- Print a long int in C using putchar() only
- Assigning multiple characters in an int in C language
- int (1 sign bit + 31 data bits) keyword in C
- Difference between pointer to an array and array of pointers
- Difference between Programmable Logic Array and Programming Array Logic
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between highest and least frequencies in an array
- Difference between List and Array in Python
- Difference between length of Array and size of ArrayList in Java
- Difference between Structure and Array in C
- Difference between String and Character array in Java
- Difference between Stack and Array
- Difference between Array, Queue and Stack