C program to reverse an array using pointers

In this article, we are going to learn the C program to reverse an array using pointers with the XOR operators we will ask users to input elements of the array and later iterate over the element of the array to reverse it.

1. C program to input elements in an array and reverse using pointers


In this code logic, we will understand how to reverse an array using pointers in C programming. The Logic we are using to reverse an array using pointers in C is using the bitwise XOR operator and taking input elements in an array from the user and reversing it using pointers.

#include <stdio.h>

#define SIZE 100 

int main()
{
    int arr[SIZE];
    int size;
	
	int *StartIndex = arr;  
    int *EndInddex;
	
      

    printf("Please enter size of array(max 100): ");
    scanf("%d", &size);
	
	EndInddex = &arr[size - 1];

    printf("Please enter elements of array:\n");
    for (int i = 0; i < size; i++)
    {
		scanf("%d", (StartIndex + i));   
    }

	printf("The elements entered by user are: \n");
    for (int k = 0; k < size; k++)
    {
        printf("Index [%d]= %d, \n",k, *StartIndex);
        StartIndex++;
    }
	
    
    StartIndex = arr;

    while(StartIndex < EndInddex) 
    {
        *StartIndex    ^= *EndInddex;
        *EndInddex   ^= *StartIndex;
        *StartIndex    ^= *EndInddex;

        StartIndex++;
        EndInddex--;
    }
	
	printf("The elements of Reversed Array are: \n");
    for (int k = 0; k < size; k++)
    {
        printf("Index [%d]= %d, \n",k, *StartIndex);
        StartIndex++;
    }



    return 0;
}

Output

Please enter size of array(max 100): 5
Please enter elements of array:
1
2
3
4
5
The elements entered by user are: 
Index [0]= 1, 
Index [1]= 2, 
Index [2]= 3, 
Index [3]= 4, 
Index [4]= 5, 
The elements of Reversed Array are: 
Index [0]= 5, 
Index [1]= 4, 
Index [2]= 3, 
Index [3]= 2, 
Index [4]= 1,