老师出的内存问题,大家说说答案(不上机前提下),并能解释一下原因~~~
Q1
#include <conio.h>
#include <stdio.h>
void GetMemory(char *p)
{
p=(char*)malloc(100);
}
void Test(void)
{
char *str=NULL;
GetMemory(str);
strcpy(str,"hello world");
printf("%s",str);
}
void main(void){
Test();
getch();
}
Q2
#include <stdio.h>
#include <conio.h>
char *GetMemory(void)
{char p[]="hello world";
return p;
}
void Test (void)
{char *str=NULL;
str=GetMemory();
printf("%s",str);
}
void main(void){
Test();
getch();
}