How to empty a char array? How to empty a char array? c c

How to empty a char array?


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


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.