How to empty a char array?

CArraysChar

C Problem Overview


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

char members[255];

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

members = "old value";

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

C Solutions


Solution 1 - C

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.

Solution 2 - C

Depends on what you mean by 'empty':

members[0] = '\0';

Solution 3 - C

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.

Solution 4 - C

char members[255] = {0};

Solution 5 - C

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.

Solution 6 - C

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

Solution 7 - C

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.

Solution 8 - C

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..

Solution 9 - C

I'd go with

members_in_use = 0;

Solution 10 - C

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);  

Solution 11 - C

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

Solution 12 - C

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");

Solution 13 - C

You can use the following instruction:

strcpy_s(members, "");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex XanderView Question on Stackoverflow
Solution 1 - CSteve GilhamView Answer on Stackoverflow
Solution 2 - CFelixView Answer on Stackoverflow
Solution 3 - CAndrejs CainikovsView Answer on Stackoverflow
Solution 4 - Cuser44556View Answer on Stackoverflow
Solution 5 - CThomas OwensView Answer on Stackoverflow
Solution 6 - CChibi.CoderView Answer on Stackoverflow
Solution 7 - CstarblueView Answer on Stackoverflow
Solution 8 - CMichael FoukarakisView Answer on Stackoverflow
Solution 9 - CpmgView Answer on Stackoverflow
Solution 10 - CsecureBadshahView Answer on Stackoverflow
Solution 11 - CTestView Answer on Stackoverflow
Solution 12 - CKonamimanView Answer on Stackoverflow
Solution 13 - Cuser8853435View Answer on Stackoverflow