clearing a char array c

CArraysChar

C Problem Overview


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.

or

my_custom_data[0] = 0; 

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

C Solutions


Solution 1 - C

It depends on how you want to view the array. If you are viewing the array as a series of chars, 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.

Solution 2 - C

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

Solution 3 - C

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.

Solution 4 - C

Use:

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

Or:

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

Solution 5 - C

Try the following code:

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

Solution 6 - C

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.

Solution 7 - C

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

Result:

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

Case 2:

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

Result:

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

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

Solution 8 - C

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.

Solution 9 - C

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?

Solution 10 - C

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.

Solution 11 - C

I usually just do like this:

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

Solution 12 - C

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

Exactly!

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

Solution 13 - C

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

Solution 14 - C

How about the following:

bzero(my_custom_data,40);

Solution 15 - C

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

Solution 16 - C

Try the following:

strcpy(my_custom_data,"");

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
Questionant2009View Question on Stackoverflow
Solution 1 - CJaredParView Answer on Stackoverflow
Solution 2 - CJohn FeminellaView Answer on Stackoverflow
Solution 3 - CabelenkyView Answer on Stackoverflow
Solution 4 - CSandyView Answer on Stackoverflow
Solution 5 - CCristian AltamiranoView Answer on Stackoverflow
Solution 6 - CJohn FrickerView Answer on Stackoverflow
Solution 7 - CAkaanthan CcoderView Answer on Stackoverflow
Solution 8 - CAlanView Answer on Stackoverflow
Solution 9 - CMichaelView Answer on Stackoverflow
Solution 10 - CtvanfossonView Answer on Stackoverflow
Solution 11 - CJevgenij KononovView Answer on Stackoverflow
Solution 12 - ChhafezView Answer on Stackoverflow
Solution 13 - CBryanView Answer on Stackoverflow
Solution 14 - CcodeconnundrumView Answer on Stackoverflow
Solution 15 - CwolfView Answer on Stackoverflow
Solution 16 - CGreg GradyView Answer on Stackoverflow