Wednesday 15 January 2014

Swapping addresses of pointers in C++ -


How can swap an indicator addresses within a function with a signature?

We say:

Full weight, height; Zero swap (int * a, int * b);

After exiting this function, the address of the actual parameter ( weight and height ) will be replaced. Is it possible at all?

If you want to swap those addresses which pointers are pointing to, not just at that address Stored values, you have to pass pointers by reference (or pointer to the pointer).

  #include & lt; Cassert & gt; Zero swap (int * and a, int * and b) {int * c = a; A = B; B = C; } Int main () {int a, b; Int * pa = & amp; A; Int * pb = & amp; B; Swap (PA, PB); Emphasis (PA == & amp; nbsp;); / / AA now stores the address of B thrombosis (PB == & amp; A); // pb now}}  

or you can use the STL swap function and pass it points.

  #include & lt; Algorithm & gt; Std :: swap (pa, pb);  

Your question is not very clear, though.


No comments:

Post a Comment