2.8指针的引用
1、指针的引用
-1.1 用一级指针引用,可以代替二级指针
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct Person
{
int m_Age;
};
void allocatMemory(Person ** p)//**p 具体的Person对象 *p是对象的指针 p指针的指针
{
*p=(Person *) malloc(sizeof(Person));
(*p)->m_Age =100;
}
void test01()
{
Person * p=NULL;
allocatMemory(&p);
cout<<"p的年龄: "<<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()
{
test02();
system("pause");
return EXIT_SUCCESS;
}