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

查看:27
本文介绍了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).

推荐答案

我建议你这样做:

*str = '';

您不需要清除缓冲区的全部内容.您可以将第一个 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天全站免登陆