What is the difference between a, &a and the address of first element a[0]? Similarly p is a pointer to an integer assigned with array’s address.
Would pointer[] do the pointer arithmetic and fetch the value as per the datatype? Further what value does * expect? Should it be a pointer ?
#include<stdio.h>
int main()
{
int a[] = {5,6,7,8};
int *p = a;
printf("\nThis is the address of a %u, value of &a %u, address of first element %u, value pointed by a %u", a, &a, &a[0], *a);
printf("\nThis is the address at p %u, value at p %u and the value pointed by p %d", &p, p, *p);
printf("\n");
}
This is the address of a 3219815716, value of &a 3219815716, address of first element 3219815716, value pointed by a 5
This is the address at p 3219815712, value at p 3219815716 and the value pointed by p 5
Note that in case of arrays(In most cases), say array
a, theADDRESS_OFais same asADDRESS_OFfirst element of the array.ie,ADDRESS_OF(a)is same asADDRESS_OF(a[0]).&is theADDRESS_OFoperator and hence in case of an arraya,&aand&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:
For more information you can refer The C Book.