#include<stdio.h>
#include<string.h>
#include<malloc.h>
void GetMemory(char **p,int num)
{
*p=(char*)malloc(num);
}
int main (void)
{
char *str=NULL;
GetMemory(&str,100);
strcpy(str,"hello");
printf("%s\n",str);
}
//二级指针可以带出申请的内存
int main()
{
char *str=(char *)malloc(100);
strcpy(str,"hello");
free(str);
if(str!=NULL)
{
strcpy(str,"world");
printf("%s\n",str);
}
}
//申请的内存释放后要指空,否则会变成野指针。输出world
char *GetMemory()
{
char p[]="hello world";
return p;
}
int main()
{
char *str=NULL;
str=GetMemory();
printf("%s\n",str);
}
//返回局部变量的值,乱码