Pointers and arrays are inseparably related, but they are not synonymous.
An array is a non empty set of sequentially indexed element having the same type of data. Each element of the array has a unique identifying index number. Changes made to one element do not affect the other elements. An array occupies contiguous block of memory.
Array notation is a form of pointer notation. The name of an array is the beginning address of the array, called the base address of the array. It is the address of the zeroth element of the array. The array name is the address constant.So *a will get the zeroth element and *(a+1) will get you the first element and so on.The indirection operator * implies that value at address a[i] is equivalent to *(a+i).
Execute this program to know the concept
printf(“%d %u %u %d %u %u %d”,a,a,&a, *a,&a,(a+1),*(a+1));
Note that a and *a will print the zeroth element that is 10.
a and &a will print the base address.
&a and (a+1) will print the address of first element.
*(a+1) will print the first element ie;20.
It is possible to assign an array to a pointer.
p=a or p=&a
The pointer to an array does not always point to the first element of the array. It can point to any element of the array.
p=a+3 will point to the third element of the array.
The array elements can be access using pointers in the usual way
p,p or *p, *(p+1)
It seems that array name and pointer, which points to the base address of the array are equivalent. But it is not true.
When the memory is allocated for array, the starting address is fixed ie; it cannot be changed during the program execution. Therefore the array name is the address constant, the value contained in it should not be changed.That is array cannot be used as lvalue but a pointer can be.
Difference between Arrays and Pointers
Arrays allocate space automatically
It cannot be resized
It cannot be re assigned
sizeof(arrayname) gives the number of bytes occupied by the array
It is explicitly assigned to point to an allocated space.
It can be resized using realloc().
It can be re assigned.
sizeof(p) returns the number of bytes used to store the pointer variable p.
The following program will read 5 numbers and print even numbers from it using pointer to array.
printf("\nenter 5 numbers...\n");
if (*(p+i)%2==0) printf("%d\n",*(p+i));