tags:

views:

178

answers:

6

Hey guys..

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  A: 
int myArray[2][2] = {};

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

Computer Guru
That's the wrong array type, and a strange-looking "zero".
Marcelo Cantos
Yeah, sorry about that. Updated.
Computer Guru
I think in C you have to write the zero (the empty braces are, however, valid C++).
avakar
+6  A: 

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.

Marcelo Cantos
This will initialize all elements to 0? Why do you say its dirty?
Ram Bhat
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.
Computer Guru
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.
Marcelo Cantos
Good point @Computer Guru; I didn't consider the ambiguity in my statement. I've amended it.
Marcelo Cantos
+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.
R..
+4  A: 
int arr[10][10] = {0}; // only in the case of 0
AraK
+4  A: 

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

Terry Mahaffey
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).
dmckee
memset uses a loop, so this doesn't answer the question.
SoapBox
`memset` might also be wrong for pointer or floating-point arrays.
Alok
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 Mahaffey
@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).
dmckee
If you use any half-decent operating system and never call `free`, `calloc` stands a decent chance of being a "O(1) zero-fill" operation. :-)
R..
+1  A: 

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.

Thomas
You could also do it with ~0
geocar
... initializing all elements to -1. Good point, hadn't thought of that.
Thomas
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`).
R..
A: 

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

c4learn.com