Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

How do I empty all the values inside a variable char array[256];?

I tried a for loop assigning an empty value at each iteration, but it doesn't compile.

share|improve this question
    
Please post this loop. –  Nick Gammon Jul 14 at 2:53
2  
You can't "empty" an array, you can only fill it with meaningless values. –  Ignacio Vazquez-Abrams Jul 14 at 3:42

2 Answers 2

up vote 1 down vote accepted
char array[256];

...

int i;
for( i=0; i<256;i++ ) {
    array[i] = 0x00;
}
share|improve this answer

There is a single-line command you can use:

memset(array, 0, 256);
share|improve this answer
    
Or memset(array, 0, sizeof array); preferably. –  Nick Gammon Jul 14 at 22:05
1  
@NickGammon Assuming you are using "array" directly, and not in a function which has a pointer passed to it. Better is to use a #define or a const int for both the array size and the memset length. –  Majenko Jul 14 at 22:34
    
Quite right, for a pointer that is correct. –  Nick Gammon Jul 14 at 23:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.