Access individual elements of array by pointers

Access individual elements of array by pointers
In the below program, an integer array num is declared and initialized. ptr is an integer pointer. Pointer ptr points to the num by the statement ptr=num;. We can access every element of an array with the pointer.

void main()
{
   int i,num[5]={10,20,30,40,50};
   int * ptr;
   clrscr();
   ptr=num;
   printf("Elements of an array with pointer:");
   for(i=0;i <=4;i++)
   printf(" %d",*&(ptr[i]));
   getche();
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.