The direct exchange address of the custom swap function is invalid

Custom swap function

We all know that the value transfer cannot change the value of the actual variable when the parameter is passed. To operate with the pointer, we have encountered a very simple problem today

  • That's what I wrote before. Pass the pointer in, and then change the value pointed to by the pointer to achieve the effect of exchange

  • The code is as follows

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void swapvalue(int *a,int *b){
	int temp;
	temp = *a;
	*a=*b;
	*b=temp;
}

int main(){
	int *a,*b;
	a=new int;
	b=new int;
	*a=3;
	*b=4;
	cout<<*a<<"  and  ";
	cout<<*b<<endl;
	cout<<a<<"   and  "<<b<<endl<<endl;	

	swapvalue(a,b);
	cout<<*a<<"  and  ";
	cout<<*b<<endl;
	cout<<a<<"   and  "<<b<<endl<<endl;

	delete a;
	delete b;

    return 0;
}
  • Program running results

It can be seen that the address does not change, and the function only exchanges the value pointed to by the pointer

  • Today I saw a binary tree exchange function, which directly uses the exchange address!!!
void Swaptree(BiTree rt) {
    if (rt) {
        if (rt->left || rt->right) {
            BiTree tmp;         //Direct exchange address
            tmp = rt->left;
            rt->left = rt->right;
            rt->right = tmp;
        }
        Swaptree(rt->left);
        Swaptree(rt->right);
    }
}

The full text is here

There is such an operation

The code is as follows
Can I exchange addresses in my custom function to achieve the effect of exchanging values???
have a try
The code is as follows

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void swapaddress(int *a, int *b) {
    int *temp;
    temp = a;
    a = b;
    b = temp;
}

int main() {
    int *a, *b;
    a = new int;
    b = new int;
    *a = 3;
    *b = 4;
    cout << *a << "  and  ";
    cout << *b << endl;
    cout << a << "   and  " << b << endl << endl;

    swapaddress(a, b);
    cout << *a << "  and  ";
    cout << *b << endl;
    cout << a << "   and  " << b << endl << endl;

    delete a;
    delete b;

    return 0;
}

Operation results

failed, what's going on

It seems that nothing has been done. Let's debug and have a look at the process

Just entered the swap function

About to exit the program

Obviously
After exiting

Why did you change back???

Can't you say that you can't pass in a pointer directly, but indirectly?? So I put it in the structure and tried again
code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

struct node {
    int *a;
    int *b;
};
typedef struct node *NODE;

void swapped(NODE t) {
    int *tmp;
    tmp = t->a;
    t->a = t->b;
    t->b = tmp;
}

int main() {
    NODE test;
    test = (NODE)malloc(sizeof(struct node));
    test->a = (int *)malloc(sizeof(int));
    test->b = (int *)malloc(sizeof(int));
    *(test->a) = 3;
    *(test->b) = 4;

    cout << *(test->a) << "  and   " << *(test->b) << endl;
	cout << test->a << "  and   " << test->b << endl<<endl;
    swapped(test);

    cout << *(test->a) << "  and   " << *(test->b) << endl;
	cout << test->a << "  and   " << test->b << endl<<endl;
    return 0;
}

result

How can it be like this again!!!!


Is the address directly passed in also a formal parameter???

  • Try the secondary pointer
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void swapSpoint(int **pa, int **pb) {
    int *temp;
    temp = *pa;
    *pa = *pb;
    *pb = temp;
}
int main() {
    int *a, *b;
    a = new int;
    b = new int;
    *a = 3;
    *b = 4;
	
    cout << *a << "  and  ";
    cout << *b << endl;
    cout << a << "   and  " << b << endl;
	cout << &a << "   and  " << &b << endl << endl;

    swapSpoint(&a,&b);

    cout << *a << "  and  ";
    cout << *b << endl;
    cout << a << "   and  " << b << endl;
	cout << &a << "   and  " << &b << endl << endl;
	

    delete a;
    delete b;

    return 0;
}

Operation results

Well, this is a successful exchange

summary

  • Have a clearer understanding of the operation of pointer passed in functions

  • I always thought that as long as the address was passed in, it was completely related to the original value

  • Even if the address is transferred to the swap function, it is actually a formal parameter, which is equivalent to copying the original address and giving two new variables in the swap function. Directly exchanging the address is equivalent to the internal value of the formal parameter (their value is the address, which is easy to mislead). After the function is completed, the original direction outside will not be affected, Then why can the "*" sign be used to exchange the values of a and b in the main function after the address is passed into the swap function? The reason is very simple. Although the address is a formal parameter in the swap function, it actually points to the real value in the physical address after combined with the * sign, so it can be changed.

  • The structure address passed in by the chestnut of the structure is also a formal parameter, but the value pointed to by this formal parameter (including the address in the structure) is real, not a copied formal parameter, so the direct exchange of addresses is effective

  • In fact, it is ignored that the address in the swap function is actually a formal parameter, but the address is unique in the machine. Its corresponding value can be operated by adding an "*" sign, which is the meaning of the pointer

  • Discover a novel
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;


int main() {
    int *a, *b;
    a = new int;
    b = new int;
    *a = 3;
    *b = 4;
	int **test;
	test = new int*;
	*test = a;
    cout << *a << "  and  ";
    cout << *b << endl;
    cout << a << "   and  " << b << endl;
	cout << &a << "   and  " << &b << endl << endl;

	cout<<&test<<"-->"<<test<<"-->"<<*test<<"-->"<<**test<<endl;

    delete a;
    delete b;
	delete test;
    return 0;
}

result

The secondary pointer also has an address

Tags: C C++ pointer

Posted on Thu, 28 Oct 2021 10:03:53 -0400 by savagenoob