#include <iostream>
#include<string.h>
using namespace std;
void GetMemory(char *p)
{
p=(char *)malloc(100);
}
int main(int argc, char *argv[])
{
char *str=NULL;
GetMemory(str);
strcpy(str,"hello world");
printf("%s\n",str);
cout << "Hello World!" << endl;
return 0;
}
此处错误在于值传递,把str传入以后,是把malloc申请的值赋给了p,又把str赋给了p,其实str并没有申请到内存,就类似于int a=10; int b=a; b=500,但