How to clear char array?

Have an array of characters such as char members [255]. How can I completely empty it without using a loop?

char members[255]; 

By "empty" I mean that if it had values ​​stored in it, it should not. For example, if I do strcat, then the old value should not remain

 members = "old value"; //empty it efficiently strcat(members,"new"); // should return only new and not "old value new" 
+48
c arrays char
Oct 13 '09 at 10:51
source share
13 answers

using

  memset(members, 0, 255); 

at all

  memset(members, 0, sizeof members); 

if the array is in scope, or

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

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




EDIT Of course, now the requirement has changed from the general task of cleaning the array to just reloading the string, memset is redundant and just resets the first element (as indicated in other answers).




EDIT To use memset, you must include string.h .

+103
Oct 13 '09 at 10:55
source share

Depends on what you mean by "empty":

 members[0] = '\0'; 
+27
Oct 13 '09 at 10:55
source share

char members [255] = {0};

+6
Oct 13 '09 at 10:55
source share

EDIT: Given the last change to the question, this will no longer work, since there is no null termination. If you try to print the array, you will get your characters, followed by the number of characters unreadable by the person. However, I am leaving this answer here as a community wiki for posterity.

 char members[255] = { 0 }; 

That should work. According to the C programming language:

If the array has a fixed size, the number of initializers cannot exceed the number of elements in the array; if there are fewer, the remaining members are initialized to 0.

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

+3
Oct 13 '09 at 10:56
source share

You cannot clear the array as such; it always contains the same amount of data.

In a larger context, the data in the array may be an empty list of elements, but this needs to be defined in addition to the array. The most common ways to do this is to keep the number of valid elements (see pmg answer) or strings to complete them with a null character (Felix answer). There are also more sophisticated methods, for example, a ring buffer uses two indexes for positions where data is added and deleted.

+3
Oct 13 '09 at 11:04
source share

Do not bother trying to nullify the char array if you are dealing with strings. Below is an easy way to work with char strings.

Copy (assign a new line):

 strcpy(members, "hello"); 

Concatenation (add a line):

 strcat(members, " world"); 

Empty line:

 members[0] = 0; 

Just.

+3
Oct 13 '09 at 13:37
source share

I would go with

 members_in_use = 0; 
+2
Oct 13 '09 at 10:54
source share
 members[0] = 0; 

enough given your requirements.

Note that this is not a buffer "empty". The memory is still allocated, valid character values ​​may exist in it, etc.

+2
Oct 13 '09 at 11:04
source share

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

+2
Nov 30 '11 at 17:05
source share

easier - better?

in this case only members [0] = 0 work. do not make the simple question so complicated.

+1
Oct 13 '09 at 13:16
source share

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

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

If you want to fill the array with another default character, you can use the memset function.

 #include <string.h> void *memset(void *s, int c, size_t n); 
0
Oct 13 '09 at 11:03
source share

Disclaimer: I usually do not program in C, so my examples can have any syntax, but I hope that the ideas I'm trying to express are clear.

If “emptying” means “containing an empty string,” you can simply set the first element of the array to zero, which effectively forces the array to contain the empry string:

 members[0] = 0; 

If “emptying” means “freeing up used memory”, 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 a string assignment) as needed.

An example of using only static lines:

 char* emptyString=""; char* members; //Set string value members = "old value"; //Empty string value member = emptyString //Will return just "new" strcat(members,"new"); 
0
Oct 13 '09 at 11:33
source share

You can use the following command:

 strcpy_s(members, ""); 
0
Oct 29 '17 at 10:04 on
source share



All Articles