Tutorial :clearing a char array c


I thought by setting the first element to a null would clear the entire contents of a char array.

char my_custom_data[40] = "Hello!";  my_custom_data[0] = '\0';  

However, this only sets the first element to null.


my_custom_data[0] = 0;   

rather than use memset, I thought the 2 examples above should clear all the data.


It depends on how you want to view the array. If you are viewing the array as a series of char's then the only way to clear out the data is to touch every entry. memset is probably the most effective way to achieve this.

On the other hand if you are choosing to view this as a C/C++ null terminated string, setting the first byte to 0 will effectively clear the string.


An array in C is just a memory location, so indeed, your my_custom_data[0] = '\0'; assignment simply sets the first element to zero and leaves the other elements intact.

If you want to clear all the elements of the array, you'll have to visit each element. That is what memset is for:

memset(&arr[0], 0, sizeof(arr));  

This is generally the fastest way to take care of this. If you can use C++, consider std::fill instead:

char *begin = &arr;  char *end = begin + sizeof(arr);  std::fill(begin, end, 0);  


Why would you think setting a single element would clear the entire array? In C, especially, little ever happens without the programmer explicitly programming it. If you set the first element to zero (or any value), then you have done exactly that, and nothing more.

When initializing you can set an array to zero:

char mcd[40] = {0}; /* sets the whole array */  

Otherwise, I don't know any technique other than memset, or something similar.



memset(my_custom_data, 0, sizeof(my_custom_data));  


memset(my_custom_data, 0, strlen(my_custom_data));  


Why not use memset()? That's how to do it.

Setting the first element leaves the rest of the memory untouched, but str functions will treat the data as empty.


Try the following code:

void clean(char *var) {      int i = 0;      while(var[i] != '\0') {          var[i] = '\0';          i++;      }  }  


Pls find below where I have explained with data in the array after case 1 & case 2.

char sc_ArrData[ 100 ];  strcpy(sc_ArrData,"Hai" );  

Case 1:

sc_ArrData[0] = '\0';  


-   "sc_ArrData"  [0] 0 ''  [1] 97 'a'  [2] 105 'i'  [3] 0 ''  

Case 2:

memset(&sc_ArrData[0], 0, sizeof(sc_ArrData));  


-   "sc_ArrData"  [0] 0 ''  [1] 0 ''  [2] 0 ''  [3] 0 ''  

Though setting first argument to NULL will do the trick, using memset is advisable


Nope. All you are doing is setting the first value to '\0' or 0.

If you are working with null terminated strings, then in the first example, you'll get behavior that mimics what you expect, however the memory is still set.

If you want to clear the memory without using memset, use a for loop.


You should use memset. Setting just the first element won't work, you need to set all elements - if not, how could you set only the first element to 0?


Writing a null character to the first character does just that. If you treat it as a string, code obeying the null termination character will treat it as a null string, but that is not the same as clearing the data. If you want to actually clear the data you'll need to use memset.


I thought by setting the first element to a null would clear the entire contents of a char array.

That is not correct as you discovered

However, this only sets the first element to null.


You need to use memset to clear all the data, it is not sufficient to set one of the entries to null.

However, if setting an element of the array to null means something special (for example when using a null terminating string in) it might be sufficient to set the first element to null. That way any user of the array will understand that it is empty even though the array still includes the old chars in memory


set the first element to NULL. printing the char array will give you nothing back.


How about the following:



I usually just do like this:

memset(bufferar, '\0', sizeof(bufferar));  


void clearArray (char *input[]){      *input = ' ';   }  


Try the following:


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »