Challenge: Swap the Numbers

Let’s practice coding with pointers in this challenge.

We'll cover the following

Problem statement

Fix the following function so that the values of the arguments that are passed to it are swapped.

For this exercise, do not specify the parameters as ref, but take them as pointers.

void swap(int lhs, int rhs) { 
    int temp = lhs;
    lhs = rhs;
    rhs = temp; 
}

Input

int i = 1; 
int j = 2;

Output

i = 2
j = 1

Challenge

This problem is designed for you to practice, so try to solve it on your own first. If you get stuck, you can always refer to the explanation and solution provided in the next lesson. Good luck!

Get hands-on with 1000+ tech skills courses.