3

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.

asked Jul 14, 2015 at 2:46
2
  • Please post this loop. Commented Jul 14, 2015 at 2:53
  • 2
    You can't "empty" an array, you can only fill it with meaningless values. Commented Jul 14, 2015 at 3:42

2 Answers 2

1
char array[256];
...
int i;
for( i=0; i<256;i++ ) {
 array[i] = 0x00;
}
answered Jul 14, 2015 at 3:30
2

There is a single-line command you can use:

memset(array, 0, 256);
answered Jul 14, 2015 at 9:13
3
  • Or memset(array, 0, sizeof array); preferably. Commented Jul 14, 2015 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. Commented Jul 14, 2015 at 22:34
  • Quite right, for a pointer that is correct. Commented Jul 14, 2015 at 23:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.