指针的引用
1 // GrabCut_328.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include<iostream> 6 using namespace std; 7 8 struct Teacher 9 { 10 char name[64]; 11 int age; 12 }; 13 14 void getTeacher(Teacher**P) 15 { 16 Teacher *tmp = NULL; 17 tmp = (Teacher*)malloc(sizeof(Teacher)); 18 tmp->age = 33; 19 //p是实参的地址 *实参的地址 去间接的修改实参的值 20 *P = tmp; 21 //return 0; 22 } 23 void getTeacher2(Teacher* &myp) 24 { 25 //给mpy赋值 相当于给Main函数中的pT1赋值 26 myp = (Teacher*)malloc(sizeof(Teacher)); 27 myp->age = 36; 28 //return 0; 29 } 30 31 int _tmain(int argc, _TCHAR* argv[]) 32 { 33 Teacher *pT1 = NULL; 34 //C语言中的二级指针 35 getTeacher(&pT1); 36 cout << "age:" << pT1->age << endl; 37 38 //C++中的引用(指针的引用) 39 getTeacher2(pT1); 40 cout << "age:" << pT1->age << endl; 41 42 system("pause"); 43 }