14

Lets say I have an array like

int arr[10][10];

Now i want to initialize all elements of this array to 0. How can I do this without loops or specifying each element?

Please note that this question if for C

1

7 Answers 7

18

The quick-n-dirty solution:

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

If you initialise any element of the array, C will default-initialise any element that you don't explicitly specify. So the above code initialises the first element to zero, and C sets all the other elements to zero.

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

5 Comments

This will initialize all elements to 0? Why do you say its dirty?
Just to clarify: It doesn't "follow suit," it forces them to zero. If you wrote = { 1 }; it would put the first value as 1, and the rest would still be zeros.
I say quick-n-dirty because, even though it is logically correct and guaranteed to work, it may confuse maintainers. I consider it a shortcut, and avoid using it unless the array or data structure is too large to conveniently initialise every element.
Good point @Computer Guru; I didn't consider the ambiguity in my statement. I've amended it.
+1 for pointing out the little-known {0}. It should be noted that the {0} initializer works for any type in the C language - integer types, floating point types, pointer types, array types (of any type), structures, unions, enums, you name it.
7

Besides the initialization syntax, you can always memset(arr, 0, sizeof(int)*10*10)

6 Comments

Note that this still take O(N) time for N elements. On the other hand it is probably a faster O(N) than the one you code by hand (or at least no slower).
memset uses a loop, so this doesn't answer the question.
memset might also be wrong for pointer or floating-point arrays.
Yes, it's technically O(n). It's unavoidable. The only way to set an arbitrary amount of bytes to the same value in constant time is with a very large magnet.
@Terry: Yes, unavoidable. I wasn't criticising your answer (indeed, you got my vote), but rather trying to prevent Shlemiel-the-painter problems for users of "high level" language users who may have understood this action as being a "single call" (I have see evidence of this issue on other posts on SO).
|
6

You're in luck: with 0, it's possible.

memset(arr, 0, 10 * 10 * sizeof(int));

You cannot do this with another value than 0, because memset works on bytes, not on ints. But an int that's all 0 bytes will always have the value 0.

2 Comments

... initializing all elements to -1. Good point, hadn't thought of that.
You can do it with any other value which, as bytes, consists of the same value in each byte. n*(UINT_MAX+1ULL)/255 is the family of such values (n=0,1,...,UCHAR_MAX).
5
int arr[10][10] = {0}; // only in the case of 0

Comments

1
int myArray[2][2] = {};

You don't need to even write the zero explicitly.

2 Comments

Yeah, sorry about that. Updated.
I think in C you have to write the zero (the empty braces are, however, valid C++).
0

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

Comments

0

Defining a array globally will also initialize with 0.


    #include<iostream>

    using namespace std;

    int ar[1000];
    
    int main(){


        return 0;
    }

1 Comment

OP asked for C, this is C++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.