Pointer swap

Hi there :)

I have question, in my assignment I have to switch string using pointers.

I done this with use of pointer to pointer.
However, i think this should be done without pointer to pointer
but i can't figure it out how this should be done.

Can I swap address that pointer one is holding with address that pointer 2 holds with
use of temporary pointer?

any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

void swap2p(char** pOne, char** pTwo);

int main() {

    char One[]{ "this is Sparta"};
    char Two[]{"this is England"};

    char* pOne = One;
    char* pTwo = Two;


    char** ppOne = &pOne;
    char** ppTwo = &pTwo;

    std::cout << "One: " << pOne << std::endl;
    std::cout << "Two: " << pTwo << std::endl;


    swap2p(ppOne, ppTwo);
    std::cout << "-------------------------" << std::endl;

    std::cout << "One: " << pOne << std::endl;
    std::cout << "Two: " << pTwo << std::endl;

    system("pause");
    return 0;
}

void swap2p(char** pOne, char** pTwo)
{
    char* tmp;
    tmp = *pOne;
    *(pOne) = *(pTwo);
    *(pTwo) = tmp;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <algorithm>

int main() {

    const char One[]{ "this is Sparta" };
    const char Two[]{ "this is England" };

    const char* pOne = One;
    const char* pTwo = Two;

    const char** ppOne = &pOne;
    const char** ppTwo = &pTwo;

    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;

    // swap pOne, pTwo using std::swap
    using std::swap ;
    std::swap( pOne, pTwo ) ; // the two pointers are passed by reference
    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;

    // swap back pOne, pTwo using std::iter_swap (on their addresses)
    std::iter_swap( ppOne, ppTwo ) ;
    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;
}

http://coliru.stacked-crooked.com/a/995217bab5790b0b
Hi JLBorges

thanks for that :) however, I was told not to use any other library apart from <iostream>
otherwise it would be too easy :D

other suggestions?

thanks :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

template < typename T > void my_swap( T& a, T& b ) 
{ auto temp{ std::move(a) } ; a = std::move(b) ; b = std::move(temp) ; } 
// { T temp(a) ; a = b ; b = c ; } // legacy C++ vesrion of my_swap

int main() {

    const char One[]{ "this is Sparta" };
    const char Two[]{ "this is England" };

    const char* pOne = One;
    const char* pTwo = Two;
    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;

    my_swap( pOne, pTwo ) ;  // swap pOne, pTwo using my_swap
    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;
}

http://coliru.stacked-crooked.com/a/ed5686c78e70a2c6
:D
well we didn't covered templates yet ;)
ok
is there a way to do this in such way

address of One is 0x61ff09
address of Two is 0x61fef9

*pOne = 0x61ff09
*pTwo = 0x61fef9

inside function I was hoping to swap this with temp pointer

*temp = 0x61ff09
*pOne = *pTwo value 0x61fef9
*pTwo = *temo with value 0x61ff09


I hope this makes sense :)
> inside function I was hoping to swap this with temp pointer

Yes; convenient if we pass the pointers by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

// uncomment if you know about function name overloading
// void my_swap( char*& a, char*& b ) { char* temp = a ; a = b ; b = temp ; } // oveload for char*

void my_swap( const char*& a, const char*& b ) { const char* temp = a ; a = b ; b = temp ; } 

int main() {

    const char One[]{ "this is Sparta" };
    const char Two[]{ "this is England" };

    const char* pOne = One;
    const char* pTwo = Two;
    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;

    my_swap( pOne, pTwo ) ;  // swap pOne, pTwo using my_swap
    std::cout << "One: " << pOne << "\nTwo: " << pTwo
              << "\n-------------------------\n" ;
}

http://coliru.stacked-crooked.com/a/7bf67689cfdc369e
Hi JLBorges

thank you for that :)

If you could tell me If my explanation what is happening makes sense :)

thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

void swap2p(char*& pOne, char*& pTwo); // <- prototype of the function
/*
My function takes reference to a pointer (so in fact address of a pointer) as argument
for 1st and 2nd value
*/


int main() {

    char One[]{ "this is Sparta"};  // <- an array of string
    char Two[]{"this is England"}; // <- an array of string

    char* pOne = One; //  <- pointer to first array of strings 
    char* pTwo = Two; //  <- pointer to second array of strings


    std::cout << "One: " << pOne << std::endl; //  printing of the original value
    std::cout << "Two: " << pTwo << std::endl;


    swap2p(pOne, pTwo); //  <- magic starts here: I am sending two elements


    std::cout << "-------------------------" << std::endl;

    std::cout << "One: " << pOne << std::endl; //  <- printing after function operation
    std::cout << "Two: " << pTwo << std::endl; //  <- printing after function operation

    system("pause");
    return 0;
}

void swap2p(char*& pOne, char*& pTwo) //  <- receiving 2 values such as 0x61ff09 and  0x61fef9 (example of memory addresses)
{
    char* tmp = pOne; // [b]<- pointer tmp is assigned with address 0x61ff09 
    pOne = pTwo; pOne is assigned with 0x61fef9 
    pTwo = tmp; pTwo is assigned with 0x61ff09 
}
Last edited on
> My function takes reference to a pointer (so in fact address of a pointer) as argument
> for 1st and 2nd value

It is best to think of a references as an alias to (just another name for) an object.
In void swap2p( char*& arg1, char*& arg2 );, arg1 and arg2 are names using which the objects which were used to initialise the references (objects that were passed by reference) can be accessed.
For this call
1
2
3
char* x = One ;
char* x = Two ;
swap2p( x, y ) ;

arg1 acts as an alis for x and arg2 acts as an alias for y; during this execution of the function, anything done to arg1 is done to x, and anything done with arg2 is done with y.
thank you for this :)
Topic archived. No new replies allowed.