Pointer and arrays:
Pointers and arrays are very closely linked in C. Consider the following statements:
int a[10], x;
int *ptr; /* ptr is a pointer variable*/
ptr = &a[0]; /* ptr points to address of a[0] */
x = *ptr;
/* x = contents of ptr (a[0] in this case) */
A pointer is a variable so we can do
ptr = a and ptr++ ;
while an array is not a variable so statements
a = pa and a++ are illegal.
A C program to display the contents of an array using a pointer arithmetic is listed below:
//display the contents of an array using pointer
#include < stdio. h >
void main()
{
int *p,sum,i;
static int x[5] = {5,9,6,3,7};
i=0;
p=x;
sum=0;
clrscr();
printf("\nElement Value Address\n\n");
while(i<5)
{
printf(" x[%d] %d %u\n",i,*p,p);
sum+=*p;
i++;
*p++;
}
printf("\n Sum = %d\n",sum);
printf("\n &x[0] = %u\n",&x[0]);
printf("\n p = %u\n",p);
getch();
}