1. References to pointers
#include <cstdlib> #include the definition of <iostream>using namespace std;//int *&LHS should be understood from right to left://LHS is a reference, with a pointer to int associated with the pin. In other words, LHS is the alias of a pointer passed into the PTRSWAP function. Note: cannot be defined as: int &*LHS, compile error prompt: Cant declare pointer to "int &" void Ptrswap (int *&lhs, int *&rhs) {int *tm p = LHS;LHS = RHS;RHS = tmp;} int main (int argc, char *argv[]) {int i = 10;int J = 20;int *pi = &i;int *PJ = &j;std::cout<< "before swap: *p i = = "<<*pi<<"; *PJ = = "<<*PJ <<"; "<<std::endl;ptrswap (PI,PJ);std::cout<<" after swap: *pi = = "<<*pi< < "; *PJ = = "<<*PJ <<"; "<<std::endl;system (" PAUSE "); return exit_success;}
2. Pointer to Pointer
#include <cstdlib> #include <iostream>using namespace std;void ptrswap (int **lhs, int **rhs) {int *tmp = *LHS; *LHS = *RHS;*RHS = tmp;} int main (int argc, char *argv[]) {int i = 10;int J = 20;int *pi = &i;int *PJ = &j;std::cout<< "before swap: *p i = = "<<*pi<<"; *PJ = = "<<*PJ <<"; "<<std::endl;ptrswap (&PI,&PJ);std::cout<<" after swap: *pi = = "< <*pi<< "; *PJ = = "<<*PJ <<"; "<<std::endl;system (" PAUSE "); return exit_success;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A reference to a pointer and a pointer to a pointer