58

Possible Duplicates:
How to initialize an array to something in C without a loop?
How to initialize an array in C

How can I zero a known size of an array without using a for or any other loop ?

For example:

arr[20] = 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;

This is the long way... I need it the short way.

Community
  • 1
  • 1
Batman
  • 1,244
  • 2
  • 14
  • 26

6 Answers6

106
int arr[20] = {0};

C99 [$6.7.8/21]

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.


Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 8
    Does this also work for resetting, not initializing, the array? – ma11hew28 Apr 26 '14 at 20:24
  • 2
    Why not just `int arr[20] = {};` without the `0`? – Stefan Pochmann May 10 '16 at 13:41
  • 1
    @StefanPochmann because it's C, not C++. – Weihang Jian Sep 23 '17 at 04:42
  • 2
    @PrasoonSaurav Nice one, but I guess it doesn't work for dynamic arrays – aderchox Jan 05 '19 at 16:41
  • 3
    I think this doesn't make it clear that it doesn't copy the element in the brackets. So `float control[16] = {15.0};` is not an array with `15.0` copied 16 times, but is one `15.0`, and the rest is 0. – David Sullivan Sep 24 '20 at 10:35
  • 1
    @WeihangJian I don't understand what's the point about being C or C++. in C I believe it's much more clear without the `0`, for the reason @DavidSullivan pointed out. It doesn't really makes sense to explicitly initialize the first element, while using default values for the others. – pheki May 04 '21 at 20:21
  • @pheki `T t = {} ;` is introduced since C++11 and they defined how it should be implemented, while the behavior is not defined in C standard and depends on each compiler's implementation. https://en.cppreference.com/w/cpp/language/zero_initialization – Weihang Jian May 05 '21 at 00:49
  • 1
    @WeihangJian It is actually defined in C, the quote in the answer (from C99 [$6.7.8/21]) is valid for both `{ 0 }` and `{ }`, since the array has 20 elements, specifying 1 or 0 elements are both ways to specify less initializers than there are members of the array. The value of the remaining elements is defined in C99 `[$6.7.8/10]`: "If an object that has static storage duration is not initialized explicitly, then: - if it has arithmetic type, it is initialized to (positive or unsigned) zero;" – pheki May 06 '21 at 02:34
83
int arr[20];
memset(arr, 0, sizeof arr);

See the reference for memset

Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
  • 11
    This can be wrong. You should either write `sizeof arr` or `20 * sizeof (int)` –  Jun 21 '13 at 09:01
  • I was confused at first, but then I remembered: "If a type name is used, it must always be enclosed in parentheses, whereas expressions can be specified with or without parentheses" (http://en.wikipedia.org/wiki/Sizeof) – taz Aug 20 '14 at 04:34
  • 14
    @BarafuAlbino How sizeof(arr) can be wrong? – Jagdish Jul 19 '15 at 11:59
  • Could it be used for non-zero values as well? I tried it and it doesn't seem so. – aderchox Jan 05 '19 at 16:55
18

Note: You can use memset with any character.

Example:

int arr[20];
memset(arr, 'A', sizeof(arr));

Also could be partially filled

int arr[20];
memset(&arr[5], 0, 10);

But be carefull. It is not limited for the array size, you could easily cause severe damage to your program doing something like this:

int arr[20];
memset(arr, 0, 200);

It is going to work (under windows) and zero memory after your array. It might cause damage to other variables values.

  • This is a perfect example of UB (undefined behaviour). The compiler can do whatever it likes when it comes across this, which usually means breaking stuff. – wizzwizz4 Feb 09 '17 at 16:47
2

int arr[20] = {0} would be easiest if it only needs to be done once.

Chris
  • 7,996
  • 11
  • 66
  • 98
1

man bzero

NAME
   bzero - write zero-valued bytes

SYNOPSIS
   #include <strings.h>

   void bzero(void *s, size_t n);

DESCRIPTION
   The  bzero()  function sets the first n bytes of the byte area starting
   at s to zero (bytes containing '\0').
drysdam
  • 8,341
  • 1
  • 20
  • 23
  • 13
    -1 *some more text from the man page:* ... This function is deprecated (marked as LEGACY in POSIX.1-2001): use memset(3) in new programs. ... – pmg Apr 12 '11 at 13:45
  • 1
    There is no header called strings.h in the C language. It is a (needless/pointless) non-standard compiler extension. – Lundin Apr 12 '11 at 14:28
1

Using memset:

int something[20];
memset(something, 0, 20 * sizeof(int));
iRaivis
  • 159
  • 3