ANSI C-清除字符串 [英] ANSI C - Clearing a string

查看:112
本文介绍了ANSI C-清除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样声明的字符串:

I've got a string declared like this:

str=malloc(sizeof(char)*128);

我想完全清除它,以便当我执行strncat()操作时,新字符将被写入str的开头.我需要清除它的原因是,我正在使用其自身的简化版本(删除多余的空白)来覆盖它.

I want to clear it completely so that whenI do strncat() operation, the new characters will be written to the beginning of str. The reason I need to clear it is that I'm writing over it with a simplified version of itself (deleting excess whitespace).

推荐答案

我建议您简单地这样做:

I suggest you simply do this:

*str = '\0';

您不需要清除缓冲区的全部内容.您可以将第一个char设置为零,然后得到一个空字符串.

You don't need to clear the entire contents of the buffer. You can just set the first char to zero and then you have the empty string.

这篇关于ANSI C-清除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆