关于指针的问题,高手一看就明白的
问题一,有下面两个程序;
1 void GetMemory(char *p)
{
p=(char*)malloc(100);
}
void main(void)
{
char *str=NULL;
GetMemory(str);
strcpy(str,"hello world");
printf(str);
}
2 void GetMemory(char **p, int num)
{
*p=(char*)malloc(num);
}
void main(void)
{
char *str=NULL;
GetMemory(&str,100);
strcpy(str,"hello world");
printf(str);
}
第一个没有结果,第二个输出正确结果,请详细解释一个二者的不同,谢谢。
问题二,程序如下
char *GetMemory(void)
{
char p[]="hello world";
return p;
}
void main(void)
{
char *str=NULL;
str=GetMemory();
printf(str);
}
为深该程序只能输出hell@这个东西,本人认为应该能输出整个hello world,请解释一下。
拜托各位了,高手一眼就能看出来,就帮小弟解决大问题了。