[c] How to empty a char array?

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"

This question is related to c arrays char

The answer is


Depends on what you mean by 'empty':

members[0] = '\0';

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


You can use the following instruction:

strcpy_s(members, "");

I had similar problem. I was trying to display results of Analog to Digital value from a Char type Array. The problem was when I was turning the Pot to get lower voltage or lower converted decimal value like 5 from between (1023 - 0), the lift over characters from array was staying beside the number 5. I used this method to get rid of the problem:

LCD_Send_String(" "); \ used spaces as string characters LCD_Send_Command (LCD_THIRD_ROW); \Returned the cursor back at the start of line.


I'd go with

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


simpler is better - make sense?

in this case just members[0] = 0 works. don't make a simple question so complicated.


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.


char members[255] = {0};


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.


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

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

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


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to char

How can I convert a char to int in Java? C# - How to convert string to char? How to take character input in java Char Comparison in C Convert Char to String in C cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)' How to get the real and total length of char * (char array)? Why is conversion from string constant to 'char*' valid in C but invalid in C++ char *array and char array[] C++ - How to append a char to char*?