GetMemory

来源:互联网 发布:linux下redis安装配置 编辑:程序博客网 时间:2024/05/03 18:59
#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;}

原创粉丝点击