如何清空字符数组?

有一个字符数组,比如 char 成员[255]。如何在不使用循环的情况下完全清空它?

char members[255];

我所说的“空”是指,如果它存储了一些值,那么它就不应该存储。例如,如果我使用 strcat,那么旧值就不应该保留

members = "old value";


//empty it efficiently
strcat(members,"new"); // should return only new and not "old value new"
495301 次浏览

I'd go with

members_in_use = 0;

char members[255] = {0};

using

  memset(members, 0, 255);

in general

  memset(members, 0, sizeof members);

if the array is in scope, or

  memset(members, 0, nMembers * (sizeof members[0]) );

if you only have the pointer value, and nMembers is the number of elements in the array.


EDIT Of course, now the requirement has changed from the generic task of clearing an array to purely resetting a string, memset is overkill and just zeroing the first element suffices (as noted in other answers).


EDIT In order to use memset, you have to include string.h.

Depends on what you mean by 'empty':

members[0] = '\0';

EDIT: Given the most recent edit to the question, this will no longer work as there is no null termination - if you tried to print the array, you would get your characters followed by a number of non-human-readable characters. However, I'm leaving this answer here as community wiki for posterity.

char members[255] = { 0 };

That should work. According to the C Programming Language:

If the array has fixed size, the number of initializers may not exceed the number of members of the array; if there are fewer, the remaining members are initialized with 0.

This means that every element of the array will have a value of 0. I'm not sure if that is what you would consider "empty" or not, since 0 is a valid value for a char.

By "empty an array" if you mean reset to 0, then you can use bzero.

#include <strings.h>
void bzero(void *s, size_t n);

If you want to fill the array with some other default character then you may use memset function.

#include <string.h>
void *memset(void *s, int c, size_t n);

You cannot empty an array as such, it always contains the same amount of data.

In a bigger context the data in the array may represent an empty list of items, but that has to be defined in addition to the array. The most common ways to do this is to keep a count of valid items (see the answer by pmg) or for strings to terminate them with a zero character (the answer by Felix). There are also more complicated ways, for example a ring buffer uses two indices for the positions where data is added and removed.

members[0] = 0;

is enough, given your requirements.

Notice however this is not "emptying" the buffer. The memory is still allocated, valid character values may still exist in it, and so forth..

Disclaimer: I don't usually program in C so there may be any syntax gotcha in my examples, but I hope the ideas I try to express are clear.

If "emptying" means "containing an empty string", you can just assign the first array item to zero, which will effectively make the array to contain an empry string:

members[0] = 0;

If "emptying" means "freeing the memory it is using", you should not use a fixed char array in the first place. Rather, you should define a pointer to char, and then do malloc / free (or string assignment) as appropriate.

An example using only static strings:

char* emptyString="";
char* members;


//Set string value
members = "old value";


//Empty string value
member = emptyString


//Will return just "new"
strcat(members,"new");

In this case just members[0] = 0 works.

Don't bother trying to zero-out your char array if you are dealing with strings. Below is a simple way to work with the char strings.

Copy (assign new string):

strcpy(members, "hello");

Concatenate (add the string):

strcat(members, " world");

Empty string:

members[0] = 0;

Simple like that.

Use bzero(array name, no.of bytes to be cleared);

You can use the following instruction:

strcpy_s(members, "");