Feb 7, 2022

swap functions - use of pointers and functions (C language)

Using C languages- swapping the values

 #include <stdio.h>
// swapping function
void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    // printf("Value of :\n c= %d   d= %d\n", a, b);
}

int main()
{
    int c = 9;
    int d = 3;

    /////////////////Pointers
    int *ptr_c;
    int *ptr_d;
    ptr_c = &c;
    ptr_d = &d;

    //////////////
    // Deferencing the values stored at pointer by using '*' at start of pointer
    printf("--->%d \n", *ptr_c);
    // Deferencing the values stored at pointer by using '*' at start of pointer
    printf("--->%d \n", *ptr_d);

    //////////////////////////
    // swapping two values ---CAll by Reference
    swap(ptr_c, ptr_d);

    printf("Value of :\n c= %d   d= %d\n", c, d);

    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...