Result of calling strcpy is different than expected

后端 未结 3 642
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 06:07
#include 
#include 

int main()
{
   char src[]=\"123456\";
   strcpy(src, &src[1]);
   printf(\"Final copied string : %s\\n\", sr         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-19 06:36

    This is undefined behaviour. Use the memmove function instead. memmove is designed to allow overlapping of source and destination buffers.

    memmove(src, &src[1], strlen(&src[1]) + 1) ;  // + 1 for copying the terminating zero
    

提交回复
热议问题