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 🙂
- Difference between pointer to an array and array of pointers
- Difference between Structure and Array in C
- Difference between pointer and array in C?
- Maximum absolute difference in an array
- Difference between highest and least frequencies in an array
- Difference between Programmable Logic Array and Programming Array Logic
- Sort an array according to absolute difference with given value using Functors
- Sum of minimum difference between consecutive elements of an array
- Difference between String and Character array in Java
- Difference between length of Array and size of ArrayList in Java
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between MP4 and MP3
- Difference between ELT and ETL
- Difference between PNG and GIF
- Difference between 4G and 5G