指针引用

指针引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<cstdlib>
using namespace std;

int my_malloc(int num,int **pp)
{

*pp = (int *)malloc(num);

return 0;
}


//指针引用做函数参数,优化二级指针
int my_malloc2(int num, int * &pp)//pp 代表 *pp
{

pp = (int *)malloc(num);

return 0;
}

int main(int argc, char* argv[])
{

int *p = NULL;

my_malloc(100, &p);

cout << p << endl;

free(p);
p = NULL;
cout << "------------------------------------" << endl;
my_malloc2(4, p);

*p = 4;
cout << *p << endl;
free(p);
p = NULL;
return 0;
}