首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟我的方法不对吗

2012-02-22 
我的方法不对吗?#include iostream.hintTest(char *pData){pDatanew char[20]return 0}int main(int

我的方法不对吗?
#include <iostream.h>
intTest(char *pData)
{
pData=new char[20];
return 0;
}

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

char *p=NULL;
Test(p);
if(p==NULL)
{
cout<<"p == NULL"<<endl;
}
else
{
cout<<"p != NULL"<<endl;
}
return 0;
}

为什么会输出 p == NULL ?
p指向的不是new[20]的地址吗?

[解决办法]
lz 修改的只是指针类型的临时变量

C/C++ code
int    Test(char **pData){*pData    =    new char[20];return 0;}
[解决办法]
所有的参数传递,其实都是copy了一个副本。所以这里必须用二级指针才行。

热点排行