Write a C Program to Reverse array using Pointers

By | 01.12.2016

Reverse array using Pointers


Write a C Program to Reverse array using Pointers. Here’s simple Program to Reverse array using Pointers in C Programming Language.


What are Pointers?


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

The general form of a pointer variable declaration is

  • type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.

The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

The unary or monadic operator & gives the “address of a variable’”.

The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.


Below is the source code for C Program to Reverse array using Pointers which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C  Program to Reverse array using Pointers  */

#include<stdio.h>
#include<conio.h>
#define MAX 30

int main()
{
   int size, i, arr[MAX];
   int *ptr;

   ptr = &arr[0];

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

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

   ptr = &arr[size - 1];

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

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

 return 0;
}

Output : :


/*  C  Program to Reverse array using Pointers  */

Enter the size of array :: 5

Enter 5 integers into array:

Enter 1 integer into array: 6

Enter 2 integer into array: 7

Enter 3 integer into array: 8

Enter 4 integer into array: 9

Enter 5 integer into array: 0

Elements of array in reverse order are :


Element 5 is 0

Element 4 is 9

Element 3 is 8

Element 2 is 7

Element 1 is 6

Process returned 0

Above is the source code for C Program to Reverse array using Pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.7 7 votes
Article Rating
Category: C Programming Pointer Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments