Access to resources in the called function

 
 

#include <iostream>

 
 

using namespace std;

 
 

struct Teacher
{
char name[64];
int age;

 
 

};

 
 

// Get resources called function
int getTeacher (Teacher ** P)
{
IF (P == NULL)
{
return -. 1;
}
Teacher * tmp = NULL;
tmp = (Teacher *) the malloc (the sizeof (Teacher));
IF (tmp == NULL)
{
return -2;
}
tmp-> Age = 20 is;
* P = tmp;
}

 
 

getTeacher1 int (* & Teacher p)
{
// assigned to p, the main function is equivalent to the assignment pt
p = (Teacher *) the malloc (the sizeof (Teacher));
IF (p == NULL)
{
return -1;
}
p-> Age = 30;
}

 
 

void FreeTeacher(Teacher* tmp)
{
if (tmp == NULL)
{
return;
}
free(tmp);
}

 
 

main void ()
{
Teacher Pt * = NULL;
// two pointer in the C language
getTeacher (& Pt);
COUT << "Age:" << Pt-> Age << endl;
FreeTeacher (Pt);

 
 

// in C ++ reference (pointer reference)
getTeacher1 (Pt);
COUT << "Age:" << Pt-> Age << endl;
FreeTeacher (Pt);
System ( "PAUSE");
}

 

 

 

Guess you like

Origin www.cnblogs.com/Yu-Weijie/p/11434230.html