从可写字符数组中删除第 i 个字符(第一个索引为 0,第二个为 1,依此类推)。您认为这是执行此任务的合法方式吗:
void delete_char(char *str, int i) {
strcpy(str + i,str + i + 1);
}
我们可以指望函数 strcpy 总是将字符从最左边的位置复制到最右边的位置吗?
最佳答案
发布的代码有未定义的行为,来自 strcpy()
:
Copies the byte string pointed to by src to byte string, pointed to by dest. If the strings overlap, the behavior is undefined. Also copies the terminating null-byte.
memmove()
可以用来完成任务,记住复制空终止符。
关于C - 从可写字符数组中删除第 i 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18078008/