以下两种都可以得到hello:
#include<stdio.h>
#include<string.h>#include<stdlib.h>
void GetMemory(char **p, int num)
{
*p = (char *)malloc(num); //p指向的值改变,即str的值改变
}
int main()
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void *GetMemory()
{
char *p = (char *)malloc(100);
return p;
}
int main()
{
char *str = NULL;
str = GetMemory();
strcpy(str, "hello");
printf(str);
return 0;
}