28

I'm beginner in C and I really need an efficient method to set all elements of an array equal zero or any same value. My array is too long, so I don't want to do it with for loop.

3
  • 1
    Just to ask -- did you indeed try using a loop? Often performance anxiety is unfounded. How many elements does your array have? How often will it be initialized? (As discussed in the answers, that will only be necessary if the array elements are to be initialized with a value other than 0.) Commented May 7, 2014 at 9:10
  • 1
    As I understand you wish to set the value to zero or any other NOT ONLY when initialized. If so this is not a duplicate. Commented Apr 25, 2019 at 22:36
  • 1
    Should not be closed as a duplicate. The referenced question asks about initialization, this one about setting which may also apply to an existing array. Commented Jan 24, 2021 at 21:03

4 Answers 4

30

If your array has static storage allocation, it is default initialized to zero. However, if the array has automatic storage allocation, then you can simply initialize all its elements to zero using an array initializer list which contains a zero.

// function scope
// this initializes all elements to 0
int arr[4] = {0};
// equivalent to
int arr[4] = {0, 0, 0, 0};

// file scope
int arr[4];
// equivalent to
int arr[4] = {0};

Please note that there is no standard way to initialize the elements of an array to a value other than zero using an initializer list which contains a single element (the value). You must explicitly initialize all elements of the array using the initializer list.

// initialize all elements to 4
int arr[4] = {4, 4, 4, 4};
// equivalent to
int arr[] = {4, 4, 4, 4};
Sign up to request clarification or add additional context in comments.

6 Comments

The standard (c99) conforming definition int x[] = { 1, 3, 5 }; does not count as initilization in one single statement?
@PeterSchneider Thanks. I corrected the confusing line.
Initialization is already discussed, many times. Question is, How would you assign same values to array?
@overexchange you can assign same values to an array only during initialization.
If your array has static storage allocation, it is NOT initialized to zero by default.
|
15

You could use memset, if you sure about the length.

memset(ptr, 0x00, length)

Comments

12
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; // All elements of myArray are 5
int myArray[10] = { 0 };    // Will initialize all elements to 0
int myArray[10] = { 5 };    // Will initialize myArray[0] to 5 and other elements to 0
static int myArray[10]; // Will initialize all elements to 0
/************************************************************************************/
int myArray[10];// This will declare and define (allocate memory) but won’t initialize
int i;  // Loop variable
for (i = 0; i < 10; ++i) // Using for loop we are initializing
{
    myArray[i] = 5;
}
/************************************************************************************/
int myArray[10] = {[0 ... 9] = 5}; // This works only in GCC

Comments

1

If your array is static or global it's initialized to zero before main() starts. That would be the most efficient option.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.