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
-
Please post this loop.Nick Gammon– Nick Gammon ♦2015年07月14日 02:53:19 +00:00Commented Jul 14, 2015 at 2:53
-
2You can't "empty" an array, you can only fill it with meaningless values.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2015年07月14日 03:42:43 +00:00Commented Jul 14, 2015 at 3:42
2 Answers 2
char array[256];
...
int i;
for( i=0; i<256;i++ ) {
array[i] = 0x00;
}
answered Jul 14, 2015 at 3:30
There is a single-line command you can use:
memset(array, 0, 256);
answered Jul 14, 2015 at 9:13
-
Or
memset(array, 0, sizeof array);
preferably.2015年07月14日 22:05:19 +00:00Commented 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 aconst int
for both the array size and the memset length.Majenko– Majenko2015年07月14日 22:34:26 +00:00Commented Jul 14, 2015 at 22:34 -
Quite right, for a pointer that is correct.2015年07月14日 23:56:45 +00:00Commented Jul 14, 2015 at 23:56
default