I have a 2d and i want to set the elements to zero without looping all the elements
int a[100][200];
I can't initialize them at point of declaration.
I have a 2d and i want to set the elements to zero without looping all the elements
int a[100][200];
I can't initialize them at point of declaration.
I tested this solution and it worked.
If this array is declared at file scope, or at function scope but with 'static', then it is automatically initialized to zero for you, you don't have to do anything. This is only good for one initialization, at program startup; if you need to reset it you have to code that yourself. I would use
memset
for that.If it's declared at function scope without static, you need to use
memset
, or an explicit initializer - a single= { 0 }
is enough, you don't need to write out all 2002 zeroes like someone else suggested.memset(a, 0, 100 * 200 * sizeof(int));
ought to do it.The
memset
approach mentioned (memset(a,0,sizeof(a));
) will work, but what about doing it the C++ way (per your tag), and using vector?Since the size is well-defined, all you need is the trailing parenthesis...
Use
This will initialize all the elements of the array with 0