小程序,小 问题

来源:百度知道 编辑:UC知道 时间:2024/05/07 06:40:21
void GetMemory( char *p, int num )
{ p = (char *)malloc(sizeof(char) * num); }
void Test(void)
{
char *str = NULL;
GetMemory(str, 100); // 尽管p得到了空间,可
str 仍然为 NULL
strcpy(str, “hello”); // 运行错误
}
谁讲一下
就是有返回值也不对

你要用GetMemory获取内存单元的大小,但是你在这个函数中并没有返回值。所以,就达不到效果了而且这个函数定义也不对。
改一下。
int GetMemory( char *p, int num )
{ return (int)malloc(sizeof(char) * num);
}
void Test(void)
{
char *str = NULL;
int a;
a=GetMemory(str, 100);
printf("%d",a);
}