C dangling pointer question

后端 未结 5 517
北海茫月
北海茫月 2020-12-16 06:46
char *xyz()
{
   char str[32];
   strcpy(str,\"Hello there!\");
   return(str);
}


void main()
{
  printf(\"%s\",xyz());
}

When I call xyz(), is i

5条回答
  •  不思量自难忘°
    2020-12-16 06:50

    Yes, it is a dangling pointer. Your program invokes undefined behaviour.

    On some systems it might crash your application, on others it might appear to work correctly. But either way, you should not do it.

提交回复
热议问题