关于C指针的问题

[复制链接]
查看11 | 回复0 | 2009-10-21 10:05:17 | 显示全部楼层 |阅读模式
楼上的说法很有道理,指针传递的其实还是指针的内容。而本例中的getMemory()函数,只是把str的地址传给p,是给p申请空间,而不是main中的str,如果想用本例中的方法给str申请空间,可以传指针的引用,如下:void getMemory(char* (&p)){
p=(char*)malloc(100);}另外貌似VC6不识别null,我把它改为了NULL。程序修改如下:#include #include using namespace std;void getMemory(char* (&p)){
p=(char*)malloc(100);}void main(){ char *str=NULL; getMemory(str); strcpy(str,"Hello World"); printf(str); printf("\n"); if(str!=NULL)free(str);}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行