C Program to Reverse Array Elements using Pointer

C Program to Reverse Array Elements using Pointer:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
 main()
 {
   int size,i,arr[50];
   int *ptr;
   ptr = &arr[0];

   printf("Enter the size of array : ");
   scanf("%d",&size);

   printf("\nEnter %d elements into array: ",size);
   for(i=0;i<size;i++)
    {
      scanf("%d",ptr);
      ptr++;
    }

   ptr=&arr[size-1];

   printf("\nElements of array in reverse order are :");

   for(i=size-1;i>=0;i--)
    {
      printf("\n%d",*ptr);
      ptr--;
    }
}

OUTPUT:

Enter the size of array : 5

Enter 5 elements into array: 7 2 3 9 5

Elements of array in reverse order are :
5
9
3
2
7

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.