educative.io

Educative

Why it is wrong ... swap values using pointer

#include

using namespace std;

void swapVals(int * p,int *q){

// Write your code here

int * temp = p;

p=q;

q=temp;

}

Hey @Krishan

I have corrected your code.Kindly check, in case of any confusion feel free to ask.

#include <iostream>
using namespace std;

void swapVals(int *p,int *q){
int  temp = *p;
*p=*q;
*q=temp;
}

int main()
{
    int a=7;
    int b=9;
    swapVals(&a, &b);
    cout <<" swapped values:" << a << " ," << b;
   return 0;
}

Happy learning,