What’s difference between “array” and “&array” for “int array[5]” ?
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.
#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; } |
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.
#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; } |
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.
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 🙂
Recommended Posts:
- Difference between pointer and array in C?
- Difference between Structure and Array in C
- Difference between highest and least frequencies in an array
- Sum of minimum difference between consecutive elements of an array
- 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?
- What is the difference between GUI and CUI?
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- What's difference between MMU and MPU?
- Difference between ++*p, *p++ and *++p
- Difference between C and C++
- What’s difference between The Internet and The Web ?
- Difference between scanf() and gets() in C
- Difference between while(1) and while(0) in C language
- Difference Between HTML and ASP