0
0
0142016-02-05 19:30:10
C++ / C#
014, 2016-02-05 19:30:10

Why doesn't the pointer swap happen?

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

void sort(const char *, const char *);
int main()
{
    const char *str[2] = {"test", "array"};
    
    cout<<str[0]<<endl;
    cout<<str[1]<<endl;

    sort(str[0], str[1]);

    cout<<str[0]<<endl;
    cout<<str[1]<<endl;


    system("pause");
    return 0;
}

void sort(const char *ch1, const char *ch2)
{
  const char *temp = ch1;
    ch1 = ch2;
    ch2 = temp;
}

as it was:
test
array
remained the same, the permutation did not happen. Tell me why this is happening.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-02-05
@014

The permutation then occurs, only locally inside the sort function.
Those. you assigned the values ​​of pointers to the function arguments, and exchanged the values ​​of the arguments with each other.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question