C Program to Swap Elements in an Array using Pointers

This is a C Program to accept an array & swap elements using pointers.

Problem Description

The program will implement an array and will swap the elements of the array. Swapping is done using pointers.

Problem Solution

1. Declare an array and define all its elements.
2. Create a function with two parameters i.e. two pointer variables.
3. Inside this function, first create a temporary variable. Then this temporary variable is assigned the value at first pointer.
4. Now, value at first pointer changes to the value at second pointer.
5. And value at second pointer changes to the value of temporary variable.
6. This way swapping is done and in main() function, we need to pass two pointer variables pointing the element which we need to swap.

Program/Source Code

Here is source code of the C program to accept an array & swap elements using pointers. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to accept an array of 10 elements and swap 3rd element
  3.      * with 4th element using pointers and display the results.
  4.      */
  5.  
  6.     #include <stdio.h>
  7.     void swap34(float *ptr1, float *ptr2);
  8.     void main()
  9.     {
  10.  
  11.         float x[10];
  12.         int i, n;
  13.  
  14.         printf("How many Elements...\n");
  15.         scanf("%d", &n);
  16.  
  17.         printf("Enter Elements one by one\n");
  18.         for (i = 0; i < n; i++) 
  19.         {
  20.             scanf("%f", x + i);
  21.         }
  22.  
  23.         /*  Function call:Interchanging 3rd element by 4th */
  24.  
  25.         swap34(x + 2, x + 3);
  26.         printf("\nResultant Array...\n");
  27.  
  28.         for (i = 0; i < n; i++) 
  29.         {
  30.             printf("X[%d] = %f\n", i, x[i]);
  31.         }
  32.  
  33.     }
  34.  
  35.     /*  Function to swap the 3rd element with the 4th element in the array */
  36.  
  37.     void swap34(float *ptr1, float *ptr2 ) 
  38.     {
  39.  
  40.         float temp;
  41.         temp = *ptr1;
  42.         *ptr1 = *ptr2;
  43.         *ptr2 = temp;
  44.  
  45.     }
Program Explanation

1. Declare an array and define all the elements according to its size taken from users.
2. Now create a function passing two pointer variables of float type as parameters to this function.
3. Inside a function a variable temp is declared of float type, which will store the value pointed by first pointer variable.
4. Now value pointer by first pointer variable is changed to the value pointed by second pointer variable, and value pointer by second variable is change to the value of temp variable.
5. Exit the function.
6. In the main() method, call this function with pointers pointing to those two element which you want to swap.

advertisement
advertisement
Runtime Test Cases
How many Elements...
4
Enter Elements one by one
23
67
45
15
 
Resultant Array...
X[0] = 23.000000
X[1] = 67.000000
X[2] = 15.000000
X[3] = 45.000000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.