64,676
社区成员
发帖
与我相关
我的任务
分享
void getMemory(char *p, int num)//不能正常输出,因为p是str的副本。p指向新内存但str仍然指向NULL
{
p = (char*) malloc(sizeof(char)*num);
};
int main()
{
char *str = NULL;
getMemory(str,100);
strcpy(str,"hello");
return 0;
}
char *getMemory(char *p, int num)//能正常输出
{
p = (char*) malloc(sizeof(char)*num);
return p;
};
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *getMemory(char *p, int num)//能正常输出
{
p = (char*) malloc(sizeof(char)*num);
return p;
}
/*
void getMemory(char *p, int num)//不能正常输出,因为p是str的副本。p指向新内存但str仍然指向NULL
{
p = (char*) malloc(sizeof(char)*num);
}*/
int main()
{
char *str = NULL;
getMemory(str,100);
strcpy(str,"hello");
printf("%s\n",str);
return 0;
}