1. #include<bits/stdc++.h>
    2. using namespace std;
    3. /**
    4. *
    5. * */
    6. int main() {
    7. int i = 9;
    8. int &ir = i;
    9. cout<<"i="<< i << "\t" << "ir=" << ir <<endl;
    10. ir = 20;
    11. cout<<"i="<< i << "\t" << "ir=" << ir <<endl;
    12. i = 12;
    13. cout<<"i="<< i << "\t" << "ir=" << ir <<endl;
    14. cout<<"address i:"<< &i <<endl;
    15. cout<<"address ir:"<<&ir<<endl;
    16. return 0;
    17. }

    输出

    1. i=9 ir=9
    2. i=20 ir=20
    3. i=12 ir=12
    4. address i:0x61fef8
    5. address ir:0x61fef8

    引用和指针的区别

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. /**
    4. * 引用注意事项
    5. * */
    6. int main() {
    7. int i = 0, a[10] = {0};
    8. // int &*ip = i; // 错误,不能建立指向引用的指针
    9. int *pi = &i;
    10. int *&pr = pi; // pi指针的引用pr
    11. *pr = 10;
    12. cout<<"i="<<i<<endl;
    13. return 0;
    14. }