指针函数
指针函数,顾名思义就是一个函数,是一个返回值为指针的一个函数,其形式:
int *fun(int a, int b);
当然也可以这样写:
int* fun(int a, int b);
让指针标准* 和 int 紧贴在一起,这样就很符合我们之前函数的书写习惯,int*是函数的返回值,fun是函数名称,(int, int)是参数列表,这个就是一个指针函数。
使用方法:
int* fun(int a, int b)
{
int* p = new int(10);
cout << "The memory address of p = " << p << endl;
cout << "*p = " << *p << endl;
memset(p, 0, sizeof(int));
*p = a + b;
cout << "*p = " << *p << endl;
return p;
}
int main()
{
int* p1 = NULL;
cout << "The memory address of p1 = " << p1 << endl;
p1 = fun(1, 2);
cout << "The memory address of p1 = " << p1 << endl;
cout << "*p1 = " <<