c++中的引用和指針小測試

#include <iostream>

using namespace std;

int main(){
    int a=1;
    int &q=a;//定義a的引用
    int *p,*p1=0;//定義一個指針p,p1
    int *&r=p;//定義一個指針的引用
    p=&a;//給p,r賦值
    *r=2; //改變a值
    //輸出結果爲a的地址
    cout<<r<<" "<<p<<" "<<&q<<" "<<&a<<endl;
     //輸出結果爲a的值
    cout<<*r<<" "<<*p<<" "<<q<<" "<<a<<endl;
    //指針自身地址
    cout<<&r<<" "<<&p<<endl;
    //p1的地址爲0
    cout<<p1<<endl;
   //報錯!找尋地址爲0的變量失敗,程序崩潰
   //cout<<*p1<<endl;

  return 0;
}

這裏寫圖片描述

發佈了39 篇原創文章 · 獲贊 2 · 訪問量 8917
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章