C语言 如何清除C中的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3831191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I clear an array in C?
提问by Kay
char x [1000];
x = 'hello';
What would I use to clear the contents of x? I'm not able to re-initialise it, use strcpy(x, '/0')or free().
我会用什么来清除内容x?我无法重新初始化它,使用strcpy(x, '/0')或free().
回答by Sven
You cannot assign anything to an array, which your variable xis. So therefore anything that starts with x =is wrong. Secondly 'hello'is not a string, it is a multicharacter literal which is of type int, so this doesn't make sense either. A string literal is enclosed by "while character (or multicharacter) literals are enclosed by '.
您不能将任何内容分配给您的变量x所在的数组。因此,任何以 开头的x =都是错误的。其次'hello'不是字符串,它是一个类型为 的多字符文字int,所以这也没有意义。字符串文字由 包围,"而字符(或多字符)文字由 包围'。
So if you want to fill your buffer xwith the string "hello" you use strncpyor even better strlcpyif available:
因此,如果您想x用字符串“hello”填充缓冲区,则可以使用strncpy或更好(strlcpy如果可用):
strncpy( x, "hello", sizeof( x ) );
strlcpy( x, "hello", sizeof( x ) );
The strlcpyfunction is better because it always terminates the string with a nulcharacter.
该strlcpy函数更好,因为它总是用一个nul字符终止字符串。
If you want to clear it you could do what the other answers suggested. I'd suggest using strncpyor strlcpywith an empty string as @codaddict suggested. That is the code that says most obviously "hey, I want to clear that string". If you want to remove the whole contents of the string from memory (for example if it contained a password or something like this) use memsetas @Ken and @Tom suggested.
如果您想清除它,您可以按照其他答案的建议进行操作。我建议使用strncpy或strlcpy使用空字符串作为@codacci 建议。那是最明显地说“嘿,我想清除那个字符串”的代码。如果您想从内存中删除字符串的全部内容(例如,如果它包含密码或类似的内容),请memset按照@Ken 和@Tom 的建议使用。
Also note that you never everuse functions like strcpyor strcatthat don't accept the size of the output buffer as a parameter. These are really not secure and cause nasty bugs and security vulnerabilities. Don't even use them if you know that nothing can go wrong, just make a habit of using the secure functions.
另请注意,您永远不会使用类似strcpy或strcat不接受输出缓冲区大小作为参数的函数。这些确实不安全,会导致令人讨厌的错误和安全漏洞。如果您知道不会出错,甚至不要使用它们,只需养成使用安全功能的习惯即可。
回答by Tom
Usage of free()would be wrong, since xis in the stack.
使用free()将是错误的,因为x它在堆栈中。
What do you mean by clear? Set it to a default value? Can you use memset? (I'm copying your code as it is)
你说的清楚是什么意思?将其设置为默认值?你能用memset吗?(我按原样复制您的代码)
#define CLEAR(x) memset(x,'x='hello';
',1000)
char x[1000];
x= 'hello';
CLEAR(x)
If not, you can always use a for loop.
如果没有,您始终可以使用 for 循环。
回答by dmckee --- ex-moderator kitten
char x[1000];
x="hello";
may not be doing what you expect because 'denotes a character constant (or in this case a multi-character constant) nota string.
可能没有做你期望的,因为'表示一个字符常量(或者在这种情况下是一个多字符常量)而不是一个字符串。
In fact, gcc won't accept the above code, complaining that 'hello'is to long (that's on a machine with 4 byte ints), and that x = 'hell'is an incompatible assignment because a char[]is not the same as an int.
事实上,gcc 不会接受上面的代码,抱怨它'hello'太长(在一台有 4 个字节int的机器上),这x = 'hell'是一个不兼容的分配,因为 achar[]与 an 不同int。
Nor should
也不应该
char x[1000];
//...
memset(x, 0, sizeof (x));
work because you can't assign arrays that way.
工作,因为您不能以这种方式分配数组。
回答by Ken
If by "clear" you mean "clear all bits in the array", how about memset(x, '\0', 1000);?
如果“清除”是指“清除数组中的所有位”,那怎么样memset(x, '\0', 1000);?
回答by No Body
To clear it, you would just reset it all to zero with memset().
要清除它,您只需使用 将其全部重置为零memset()。

