读书人

看一下这个C程序多谢

发布时间: 2013-09-07 14:12:44 作者: rapoo

看一下这个C程序,谢谢!


void getmemory(char *p)
{
p=(char *) malloc(100);
strcpy(p,“hello world”); }
int main( )
{
char *str=NULL;
getmemory(str);
printf(“%s/n”,str);
free(str);
return 0; }

会出现什么问题? malloc c
[解决办法]
getmemory没能给str分配内存,getmemory返回之后,str还是空的。
可以这么改

void getmemory(char **p)
{
*p=(char *) malloc(100);
strcpy(*p,"hello world");
}

char *str=NULL;
getmemory(&str);
printf("%s/n",str);
free(str);

读书人网 >C语言

热点推荐