Pointers – Difference between Array and Pointer

int a[]={5,6,7,8};
int *p= a;

Note that in case of arrays(In most cases), say array a, the ADDRESS_OF a is same as ADDRESS_OF first element of the array.ie, ADDRESS_OF(a) is same as ADDRESS_OF(a[0]). & is the ADDRESS_OF operator and hence in case of an array a, &a and &a[0] are all the same.

I have already emphasized that in most cases, the name of an array is converted into the address of its first element; one notable exception being when it is the operand of sizeof, which is essential if the stuff to do with malloc is to work. Another case is when an array name is the operand of the & address-of operator. Here, it is converted into the address of the whole array. What’s the difference? Even if you think that addresses would be in some way ‘the same’, the critical difference is that they have different types. For an array of n elements of type T, then the address of the first element has type ‘pointer to T’; the address of the whole array has type ‘pointer to array of n elements of type T’; clearly very different.

Here’s an example of it:

int ar[10];
int *ip;
int (*ar10i)[10];       /* pointer to array of 10 ints */

ip = ar;                /* address of first element */


ip = &ar[0];            /* address of first element */
ar10i = &ar;            /* address of whole array */

For more information you can refer The C Book.

Leave a Comment