天天看点

指针的引用作形参

// 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;
}