What is the output of the main function? Can you explain why?char *GetMemory(void){ char p[] = “hello world”; return p;} void Test(void){ char *str = NULL; str = GetMemory(); printf(str);}
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/22 18:11:30
What is the output of the main function? Can you explain why?char *GetMemory(void){ char p[] = “hello world”; return p;} void Test(void){ char *str = NULL; str = GetMemory(); printf(str);}
What is the output of the main function? Can you explain why?
char *GetMemory(void)
{
char p[] = “hello world”;
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);}
What is the output of the main function? Can you explain why?char *GetMemory(void){ char p[] = “hello world”; return p;} void Test(void){ char *str = NULL; str = GetMemory(); printf(str);}
程序中有点小问题.
结果是乱码.
因为数组存放在栈中,随着函数GetMemory执行完成,栈空间已经释放,被用作其他用途.当指针str指向已释放的数组时,其内容不确定,所以乱码