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/05/06 02:27:17
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);}

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指向已释放的数组时,其内容不确定,所以乱码