A reference to a pointer and a pointer to a pointer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.