天天看點

指針的引用作形參

// pointerreferenceasfunctionparameter.cpp : Defines the entry point for the console application.
//

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

typedef struct pos
{
	int x;
	int y;
}pos;

void func(pos *&p)// 指針的引用
{
	p->x++;
	p->y++;
}

int _tmain(int argc, _TCHAR* argv[])
{
	pos p;
	cin>>p.x>>p.y;

	pos *q = &p;
	func(q);// 注意不可以直接傳&p,隻能傳指針q
	// 這是因為p是一個已經在棧中擁有固定存儲位置的變量,傳&p等于是傳其位置。
	// 然而其位置已經固定不可改變,是以不能傳其位置,應傳其指針,即傳q。

	cout<<p.x<<' '<<p.y<<'\n';
	return 0;
}