简体   繁体   English

无法清除char数组

[英]Can't clear char array

My C application reads a file character by character and stores the char value in a char array. 我的C应用程序逐个字符读取文件,并将char值存储在char数组中。 At a certain point, the char array needs to be cleared so another value can be entered. 在某个时候,需要清除char数组,以便可以输入另一个值。 However, when I try to clear it, the chars still remain in the array. 但是,当我尝试清除它时,字符仍然保留在数组中。

This is how I reset the array: 这是我重置数组的方式:

void resetArray(){
    operand[0] = '\0';
}

What am I doing wrong? 我究竟做错了什么?

Setting the first character as the null terminator will leave the rest of the array in memory intact. 将第一个字符设置为空终止符将使数组的其余部分完整保留在内存中。 Printing the string will not print them, but they are still there. 打印字符串不会打印它们,但是它们仍然存在。

You have to clear the remaining memory. 您必须清除剩余的内存。

memset( array , 0 , sizeof( array ) ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM