Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

swap using pointer in c

// swap the value of varible using pointer
#include <stdio.h>
void swap(int *, int *);
int main()
{
    int a = 11;
    int b = 5;
    int *p, *q;
    p = &a; // p holds the address of a
    q = &b; // q holds the address of b
    printf("Before swapping value of a = %d , b = %d 
", *p, *q);
    swap(p, q); // you can use - swap(&a,&b);
    printf("After swapping value of a = %d ,b = %d
", *p, *q);
    return 0;
}
// function that swaps the value of the integer variable
void swap(int *p, int *q)
{
    int t;
    t = *p;
    *p = *q;
    *q = t;
}
 
PREVIOUS NEXT
Tagged: #swap #pointer
ADD COMMENT
Topic
Name
1+8 =