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.

  • Please post this loop. – Nick Gammon Jul 14 '15 at 2:53
  • 2
    You can't "empty" an array, you can only fill it with meaningless values. – Ignacio Vazquez-Abrams Jul 14 '15 at 3:42
char array[256];


int i;
for( i=0; i<256;i++ ) {
    array[i] = 0x00;

There is a single-line command you can use:

memset(array, 0, 256);
  • Or memset(array, 0, sizeof array); preferably. – Nick Gammon Jul 14 '15 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 '15 at 22:34
  • Quite right, for a pointer that is correct. – Nick Gammon Jul 14 '15 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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