清除字符数组c [英] clearing a char array c

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

问题描述

我认为通过第一元件设置为空会清除字符数组的全部内容

I thought by setting the first element to a null would clear the entire contents of a char array.

char my_custom_data[40] = "Hello!";
my_custom_data[0] = '\0';

不过,这只是设置为空的第一个元素。

However, this only sets the first element to null.

my_custom_data[0] = 0; 

而不是使用 memset的,我认为上面的两个例子应该清除所有的数据。

rather than use memset, I thought the 2 examples above should clear all the data.

推荐答案

这取决于你想如何查看阵列。如果您正在查看该数组作为一系列字符的然后清除数据的唯一的方法是触摸每个条目。 memset的可能是实现这一目标的最有效途径。

It depends on how you want to view the array. If you are viewing the array as a series of char's then the only way to clear out the data is to touch every entry. memset is probably the most effective way to achieve this.

在另一方面,如果你是选择认为这是一个C / C ++空结尾的字符串,第一个字节设置为0,将有效地清除字符串。

On the other hand if you are choosing to view this as a C/C++ null terminated string, setting the first byte to 0 will effectively clear the string.

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

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