Swapping Numbers with Pointers

CCBeginner
Practice Now

Introduction

In C programming, you can swap two numbers using pointers. Pointers are powerful features in C that allow us to manipulate data by storing and accessing addresses instead of actual values. In this lab, we will learn how to swap two numbers using pointers.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/comments -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/variables -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/data_types -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/operators -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/user_input -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/memory_address -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/pointers -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} c/function_declaration -.-> lab-123350{{"`Swapping Numbers with Pointers`"}} end

Declare Variables

First, declare two integer variables a and b. Then, declare two integer pointers ptra and ptrb that will hold the addresses of variables a and b respectively. Lastly, declare an integer variable temp that will temporarily hold the value of a or b during the swap.

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    /* Your code here */

    return 0;
}

Read Input

Next, read in the value of a and b from the user.

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    printf("Enter value for a: ");
    scanf("%d", &a);

    printf("Enter value for b: ");
    scanf("%d", &b);

    /* Your code here */

    return 0;
}

Print Original Values

Print the original values of a and b.

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    printf("Enter value for a: ");
    scanf("%d", &a);

    printf("Enter value for b: ");
    scanf("%d", &b);

    printf("Original values: a = %d, b = %d\n", a, b);

    /* Your code here */

    return 0;
}

Get Addresses

Get the addresses of a and b by assigning them to ptra and ptrb, respectively.

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    printf("Enter value for a: ");
    scanf("%d", &a);

    printf("Enter value for b: ");
    scanf("%d", &b);

    printf("Original values: a = %d, b = %d\n", a, b);

    ptra = &a;
    ptrb = &b;

    /* Your code here */

    return 0;
}

Swap Two Numbers

Swap the values of a and b using pointers.

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    printf("Enter value for a: ");
    scanf("%d", &a);

    printf("Enter value for b: ");
    scanf("%d", &b);

    printf("Original values: a = %d, b = %d\n", a, b);

    ptra = &a;
    ptrb = &b;

    temp = *ptra;
    *ptra = *ptrb;
    *ptrb = temp;

    /* Your code here */

    return 0;
}

Print Swapped Values

Print the swapped values of a and b.

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    printf("Enter value for a: ");
    scanf("%d", &a);

    printf("Enter value for b: ");
    scanf("%d", &b);

    printf("Original values: a = %d, b = %d\n", a, b);

    ptra = &a;
    ptrb = &b;

    temp = *ptra;
    *ptra = *ptrb;
    *ptrb = temp;

    printf("Swapped values: a = %d, b = %d\n", a, b);

    return 0;
}

Write Full Code

The full code for the program is as follows:

#include <stdio.h>

int main()
{
    int a, b;
    int *ptra, *ptrb;
    int temp;

    printf("Enter value for a: ");
    scanf("%d", &a);

    printf("Enter value for b: ");
    scanf("%d", &b);

    printf("Original values: a = %d, b = %d\n", a, b);

    ptra = &a;
    ptrb = &b;

    temp = *ptra;
    *ptra = *ptrb;
    *ptrb = temp;

    printf("Swapped values: a = %d, b = %d\n", a, b);

    return 0;
}

Summary

In this lab, we learned how to swap two numbers using pointers. We declared two integer variables and two integer pointers. We then got the addresses of the two variables by assigning them to the pointers. Lastly, we swapped the values of the two variables by using pointers.

Other C Tutorials you may like