免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11297 | 回复: 5
打印 上一主题 下一主题

[C] How to del a character from a string? [复制链接]

论坛徽章:
0
1 [报告]
发表于 2008-04-08 10:59 |显示全部楼层
c = 'e';
cp = strchr(str, c);
if(cp)
    strcpy(cp, cp+1);

论坛徽章:
0
2 [报告]
发表于 2008-04-08 12:13 |显示全部楼层
能不能overlap, 你自己不会实验一下吗?

那么简单的程序.


原帖由 放驴娃 于 2008-4-8 11:35 发表

use memmove to instead.

论坛徽章:
0
3 [报告]
发表于 2008-04-08 12:20 |显示全部楼层
文档如果能看清楚真正含义, 当然按文档.

原帖由 放驴娃 于 2008-4-8 12:16 发表

窃以为,听文档的话显得比较 professional;
相应的,“实验一下”显得太不 professional。

论坛徽章:
0
4 [报告]
发表于 2008-04-08 12:33 |显示全部楼层
谁告诉你的, not allowed?

看man strcpy如何说的? may not, 不等于not allowed.

The strings  may not overlap, and the destination string dest must be large
       enough to receive the copy.

原帖由 77h2_eleven 于 2008-4-8 12:29 发表

yes!

论坛徽章:
0
5 [报告]
发表于 2008-04-08 17:11 |显示全部楼层
没有问题的.
man strcpy说
The strings  may not overlap, and the destination string dest must be large
enough to receive the copy.

不是一定不能overlap. 实际这样使用, 字符一个又一个COPY, 内部没有破坏性overlap发生.

你可以实验, 在所有系统上都应该可以. 因为不可能有strcpy的实现从后面往前拷.


原帖由 cjaizss 于 2008-4-8 16:43 发表

这样们使用不符合标准

论坛徽章:
0
6 [报告]
发表于 2008-04-09 09:12 |显示全部楼层
也同意你, 根据标准小心就是了.

(strcpy) may not 准确翻译不应该是"不可以". 而是, 可能不可以. 就是有的时候不可以, 有时候可以.
(memcpy) The memory areas should not overlap 才是"不可以", "不应该". "应该不". 任何时候都不
(memmove) The memory areas may overlap 可能.

否则你如何区分同一个库中的"may not", "should not"?

-------------------
原帖由 whyglinux 于 2008-4-8 23:10 发表
cjaizss  的意见是正确的。

man 中写得很清楚:The strings  may not overlap,翻译过来就是:(这两个)字符串不可以重叠。如果拷贝的空间重叠了,C 标准的规定也很明确:产生无定义行为。

>> 你可以实验 ...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP