關於指針的一些問題追究

關於指針的一些問題,直接上代碼和運行結果,一目瞭然:

// 指針.cpp : 定義控制檯應用程序的入口點。
#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
	double *p;//定義一個指向double型變量的指針p,裏面裝的是一個地址
	double i; //一個double形數i;
	cout<<"output &p "<<p<<endl;//打印指針的地址,此時還未指定指向,是隨機值。
  //cout<<"output *p "<<*p<<endl;  //*p沒有被初始化,c++不容許打印。	
	cout<<"output &i "<<&i<<endl;
	p=&i; //把i的地址賦值給p,從此p裏面裝的是i的地址。
	cout<<"output &p "<<p<<endl; //已經指定指向地址,和&(*p)是一個意思,都是取其p指向的i地址
	cout<<"output int i(未賦值)="<<i<<endl;
	i=10;
	cout<<"output int i(已賦值)="<<i<<endl;
	cout<<"output int &i(求取i地址)="<<&i<<endl;//&i,&p,&(*p),代表的都是一樣的地址值
	cout<<"output int *p(驗證p指向地址的實際變量值)="<<*p<<endl;//已經初始化,驗證*p代表的是指向的地址的值
	cout<<"output &*p(求取*p的地址)="<<&(*p)<<endl;
	cout<<"output &p(求取p的地址)="<<&p<<endl; //存放指針本身的地址
	cout<<"output typeof i:"<<sizeof (i)<<endl;
	cout<<"output typeof p:"<<sizeof (p)<<endl;//指針變量本身永遠是unsigned long int型,佔4字節。
	cout<<"output typeof *p:"<<sizeof (*p)<<endl;//	
	system("pause");
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章