1. 程式人生 > >【小練習】指標與引用:基本問題

【小練習】指標與引用:基本問題

1.練習程式碼-找出錯誤

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int iv;
	int iv2 = 1024;
	int iv3 = 999;
	int &reiv;
	int &reiv2 = iv;
	int &reiv3 = iv;
	int *pi;
	*pi = 5;
	pi = &iv3;
	const double di;
	const double maxWage = 10.0
; const double minWage = 0.5; const double *pc = &maxWage; cout << pi; return 0; }

2.關鍵點分析

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int iv;
	int iv2 = 1024;
	int iv3 = 999;
	int &reiv; //錯誤1:聲明瞭一個引用,沒有初始化
	int &reiv2 =
iv; int &reiv3 = iv; int *pi; *pi = 5; //錯誤2:未給指標宣告指向的地址,直接賦值,造成混亂,值不知道賦到哪裡 pi = &iv3; const double di; //錯誤3:聲明瞭一個const常量,未初始化 const double maxWage = 10.0; const double minWage = 0.5; const double *pc = &maxWage; cout << pi; return 0; }