c++ (指针的引用)

struct Person
{
	int m_Age;
};
void allocatMemory(Person **p) //**p具体person本体 *p对象的指针 p指针的指针
{
	*p = (Person *)malloc(sizeof(Person));
	(*p)->m_Age = 200;
}
void test01()
{
	Person *p = NULL;
	allocatMemory(&p);
	cout << "Person is age: " << p->m_Age << endl;
}
//利用指针引用开辟空间
void allocatMemoryByRef(Person* &p)
{
	p =(Person *)malloc(sizeof(Person));
	p->m_Age = 1000;
}
void test02()
{
	Person *p = NULL;
	allocatMemoryByRef(p);
	cout << "p的年龄" << p->m_Age << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}

posted on 2021-04-21 19:00  lodger47  阅读(42)  评论(0)    收藏  举报

导航