Result of calling strcpy is different than expected

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

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


        
3条回答
  •  猫巷女王i
    2021-01-19 06:46

    strcpy(src, &src[1]); is undefined behavior:

    C11 §7.24.2.3 The strcpy function

    The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

    By the way, memcpy is similar (but not memmove). See C FAQ: What's the difference between memcpy and memmove.

提交回复
热议问题