0

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.

  • 3
    Passing pointers is an old way to emulate pass by reference in C. It's not needed in C++ since C++ have references. – Some programmer dude Oct 04 '22 at 10:36
  • 2
    On another note, [`using namespace std;` is not a good habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Think about your `swap` function and then about [`std::swap`](https://en.cppreference.com/w/cpp/algorithm/swap). – Some programmer dude Oct 04 '22 at 10:37
  • 1
    I am not sure what are you asking at the end. Have you tried stepping through the code with a debugger, inspecting the addresses and seeing how they change? Maybe you can draw memory cells with arrows representing the pointers and step through the code on a paper. – Quimby Oct 04 '22 at 10:38
  • 2
    As for the pointer issues, I recommend you bring out some pen and paper, and draw it all out. Draw labeled boxes for all variables, and arrows between them to represent the pointers. Whenever you use the dereference operator `*` follow the arrow to where it points, and when you use the pointer-to operator `&` go back the "wrong" way over the arrow. – Some programmer dude Oct 04 '22 at 10:39
  • If you're wondering why passing either `&pa` and `ppa` produce the same result, note that `int **ppa=&pa;` says that the value of `ppa` *is* `&pa`; you're passing the same value to the function both cases. A pointer to a pointer is no different from a pointer to anything else. (The big secret of pointers is that there is nothing special about them.) – molbdnilo Oct 04 '22 at 11:24
  • no i what i meant was that it gave me the same result whether i gave as input pa or &pa. – ben tenyson Oct 08 '22 at 05:56

1 Answers1

0

Passing pointer-to-pointer or passing address of pointer are giving the same result because pointer-to-pointer is also storing the address of pointer.

so passing address directly using & or passing pointer will give the same output.

Pointer: A pointer is a variable that stores the memory address of another variable

int a = 5;
int *ptr = &a;
int **pptr = &ptr;

std::cout << pptr;     // pptr: 0x77bcdff7d0
std::cout << &ptr;     // &ptr: 0x77bcdff7d0

pointer-to-pointer is defined as int **ppa = &pa so swap(&pa, &pb) and swap(ppa, ppb) will give the same ouput

Sidharth Mudgil
  • 1,293
  • 8
  • 25