指针的引用

#include<iostream>
using namespace std;

struct Person {
	int m_age;
};

//**p 具体的Person对象  *p 对象的指针  p 指针的指针
void allocatMemory(Person **p) {
	*p = (Person*)malloc(sizeof(Person));
	(*p)->m_age = 63;
}

void test01() {
	Person *p = NULL;
	allocatMemory(&p);
	cout << "p的年龄:" << p->m_age << endl;
}

int main() {
	test01();

	system("pause");
	return 0;
}

利用指针引用来开辟空间

#include<iostream>
using namespace std;

struct Person {
	int m_age;
};

void allocatMemorryByRef(Person* &p) {
	p = (Person*)malloc(sizeof(Person));
	p->m_age = 1000;
}

void test02() {
	Person *p = NULL;
	allocatMemorryByRef(p);
	cout << "p的年龄:" << p->m_age << endl;
}

int main() {
	test02();

	system("pause");
	return 0;
}

版权声明:本文为Scarlett2025原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。