67

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.

3
  • also stackoverflow.com/questions/201101/… Commented Apr 12, 2011 at 13:46
  • 13
    Your "example" does not do what you think it does. Commented Apr 12, 2011 at 13:49
  • 3
    The code you posted isn't valid C. Try posting something that compiles. Commented Apr 12, 2011 at 14:27

6 Answers 6

110
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.


Sign up to request clarification or add additional context in comments.

8 Comments

Does this also work for resetting, not initializing, the array?
Why not just int arr[20] = {}; without the 0?
@StefanPochmann because it's C, not C++.
@PrasoonSaurav Nice one, but I guess it doesn't work for dynamic arrays
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.
|
86
int arr[20];
memset(arr, 0, sizeof arr);

See the reference for memset

4 Comments

This can be wrong. You should either write sizeof arr or 20 * sizeof (int)
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" (en.wikipedia.org/wiki/Sizeof)
@BarafuAlbino How sizeof(arr) can be wrong?
Could it be used for non-zero values as well? I tried it and it doesn't seem so.
20

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.

1 Comment

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.
2

Using memset:

int something[20];
memset(something, 0, 20 * sizeof(int));

2 Comments

Even better: int something[20]; memset(something, 0, sizeof something);
unsure what OP wanted to ask, but he asked how to zero an array, not how to init, and this answer the question.
2

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

Comments

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').

2 Comments

-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. ...
There is no header called strings.h in the C language. It is a (needless/pointless) non-standard compiler extension.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.