|
下面看指针函数的实现:
int *f(int a, int b); // 声明指针函数
int _tmain(int argc, _TCHAR* argv[])
{
printf("------------------------------ Start\n");
int *p1 = NULL;
printf("The memeory address of p1 = 0x%x \n", p1);
p1 = f(1, 2);
printf("The memeory address of p1 = 0x%x \n", p1);
printf("*p1 = %d \n", *p1);
printf("------------------------------ End\n");
getchar();
return 0;
}
/*
* 指针函数的定义
* 返回值是指针类型int *
*/
int *f(int a, int b) {
int *p = (int *)malloc(sizeof(int));
printf("The memeory address of p = 0x%x \n", p);
memset(p, 0, sizeof(int));
*p = a + b;
printf("*p = %d \n", *p);
return p;
}
通过运行结果,可以看出,指针函数f返回的类型是一个指针类型,因为f是赋值给int类型指针p1的,如果不是指针类型,编译就会出错。
|
|