Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C dangling pointer question

Tags:

c

char *xyz()
{
   char str[32];
   strcpy(str,"Hello there!");
   return(str);
}


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

When I call xyz(), is it will return a dangling pointer ? Thanks

like image 269
Josh Morrison Avatar asked Mar 11 '11 22:03

Josh Morrison


1 Answers

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.

like image 83
Mark Byers Avatar answered Sep 28 '22 02:09

Mark Byers