So I had this task of swapping the values of two pointers themselves. The question said that i should i make it so that the function takes as an input the address of the pointer variables and then swaps em. This is how i did it
#include<iostream>
using namespace std;
void swap(int **ptra, int **ptrb)
{
int* temp = *ptra; // declares a temp pointer to hold the value of pointer a
*ptra = *ptrb; // pointer a is given the value of pointer b
*ptrb = temp; // pointer b is given the value of temp (which had the value of pointer a)
}
int main(){
int a=5, b=10;
int *pa=&a; //pa and pb are pointer variables of type int.
int *pb=&b;
int **ppa=&pa; //ppa and ppb are called double pointers or pointers-to-pointers.
int **ppb=&pb;
cout << "before" <<pa<< endl;
cout << "before" <<pb <<endl;
swap(&pa,&pb);
cout << pa<<endl;
cout << pb<< endl;
}
I am having trouble understanding why the swap function works returns same output if i put pa or &pa.