#include
#include
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;
}
不能输出正确的结果是为什么
#include
#include
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;
}
不能输出正确的结果是为什么
char **str=NULL;//此处声明错误,应声明为char *str=NULL,否则就是在NULL上放指针地址,
如果想不变动其他代码,可以将NULL改成合法地址,但应该没人会那样写。
正确写法如下:
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;
}