Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to initialize a one-dimensional array to all zeros with C language, without loops?
How to initialize a one-dimensional array to all zeros with C language, without loops?
The following three methods:

1 . int arr[ 10]= { 0 };

If only a part of the array is initialized, the compiler will set other elements to 0. So only when the first element is initialized to 0, the compiler will automatically set other elements to 0 (as the previous sentence said).

2.int arr[3 1]= { };

3. Initialize the array with the memset function at the beginning of the program. For example:

int arr[ 1024];

memset(arr,0, 1024); //Clear

Extended data:

Memset () function prototype is externvoid * memset (void * buffer, int c, int count) buffer: is a pointer or array, c: is the value assigned to buffer, and count: is the length of buffer.

Function explanation: replace n bytes (typedef unsigned int size_t) after the current position in S with ch, and return S.

Memset: used to fill the given value in the memory block. This is the fastest way to clear large structures or arrays, and it usually initializes the memory of new applications.

References:

Memset- Baidu encyclopedia