#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void GetMemory(char **p)
{
*p = (char*)malloc(100);
}
void GetMemory_1(char *p)
{
p=(char *)malloc(100);
}
void GetMemory_2(char **p)
{
// 这条语句编译出错,将一个二级指针指向分配的地址了
// p = (char*)malloc(100);
// 可以使用强制转换,但程序crash
p = reinterpret_cast<char**>(malloc(100));
}
int main(int argc, char *argv[])
{
char *str = NULL;
//GetMemory_2(&str);
GetMemory(&str);
strcpy(str, "Hello");
free(str);
if (str!=NULL)
{
strcpy(str,"wrold");
}
printf("%s",str);
return 0;
}GetMemory
最新推荐文章于 2025-07-20 00:59:22 发布
908

被折叠的 条评论
为什么被折叠?



