#include <iostream>
int main()
{
const int * p2 = (int *)9;
std::cout << (int)p2 << std::endl;
p2 = new int[10];// p2改变了,不为9
std::cout << (int)p2 << std::endl;
const int * p3 = p2;
p3 = new int[10];// p3改变了,不为p2
std::cout << (int)p3 << std::endl;
delete [] p2;
delete [] p3;
// malloc同上
//int * p2 = (int *)9;
//std::cout << (int)p2 << std::endl;
//p2 = (int *)malloc(sizeof(int) * 10);
//std::cout << (int)p2 << std::endl;
//int * p3 = p2;
//p3 = (int *)malloc(sizeof(int) * 10);
//std::cout << (int)p3 << std::endl;
//free(p2);
//free(p3);
return 0;
}
因此,函数指针作为参数传参时,如果函数体中对指针进行开辟空间操作时,要考虑用二重指针(int ** ppa)或者指针引用(int *& pa)。