Result of calling strcpy is different than expected

只愿长相守 提交于 2019-12-01 18:42:22

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.

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

From ISO/IEC 9899:TC3 (c99)

7.21.2.3 The strcpy function

Synopsis

1

#include <string.h>

char *strncpy(char * restrict s1, const char * restrict s2, size_t n);

Description

2 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.

So what you are doing is simply undefined behaving ;)

You can also see the ANNEX J.2

Stating cases of undefined behavior with a note how to prevent:

The behavior is undefined in the following circumstances:

[...]

—An attempt is made to copy an object to an overlapping object by use of a library function, other than as explicitly allowed (e.g., memmove) (clause 7).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!