logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program to Accept an Array & Swap Elements using Pointers

Posted on November 7, 2012 by Manish

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.

advertisement
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
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 Reference Books in C Programming, Data Structures and Algorithms.

« Prev Page - C Program to Find the Size of a Union
» Next Page - C Program to Cyclically Permute the Elements of an Array

« C Program to Find the Size of a Union
C Program to Cyclically Permute the Elements of an Array »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Combinatorial Problems & Algorithms
  2. C# Programming Examples on Matrix
  3. C Tutorials
  4. C Programming Examples on Bitwise Operations
  5. C Programming Examples on Linked List
  6. C# Programming Examples on Sorting
  7. C Questions and Answers
  8. C# Programming Examples on Arrays
  9. C Programming Examples on Searching and Sorting
  10. C Programming Examples on Arrays
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.