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

看一下这个C程序,多谢

2013-09-07 
看一下这个C程序,谢谢!void getmemory(char *p) { p(char *) malloc(100) strcpy(p,“hello world”) } i

看一下这个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);

热点排行