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.
int arr[20];
memset(arr, 0, sizeof arr);
See the reference for memset
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').
int arr[20] = {0}
would be easiest if it only needs to be done once.
Using memset
:
int something[20];
memset(something, 0, 20 * sizeof(int));
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.
Source: Stackoverflow.com