14

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.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
user54871
  • 141
  • 1
  • 1
  • 3

12 Answers12

31

Try

int a[100][200] = {{0}};

If you initialize some (but not all) of the elements, the rest will be initialized to zero. Here, you are only explicitly initializing the first element of the first sub-array and the compiler is taking care of the rest.

bta
  • 43,959
  • 6
  • 69
  • 99
18

Try memset(a,0,sizeof(a));

This simply overwrites the memory used by the array with 0 bytes. Don't do this for user-defined data types unless you really know what you do. There's also std::fill and std::fill_n, which is more C++ish (but not the easiest way here).

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 2
    Even for non-user defined data-types, this may generate unexpected results. Data member-pointers are an example that use a non-0x0 null-pointer encoding in the C++-ABI used by GCC and Clang ("Itanium ABI") - they use all-1 bits. For "int" this should be fine though. – Johannes Schaub - litb Aug 27 '10 at 20:55
  • Shouldn't you use `sizeof(a) * sizeof(int)` here? – Mikhail Dec 02 '12 at 22:45
  • 2
    No, sizeof on the array returns the correct size in bytes, so there is no need to multiply by the int size. – Alexander Gessler Dec 03 '12 at 02:18
7

For C++, you can use the std:fill method from the algorithm header.

int a[x][y];
std::fill(a[0], a[0] + x * y, 0);

So, in your case, you could use this:

int a[100][200];
std::fill(a[0], a[0] + 100 * 200, 0);

Of course, the 0 could be changed for any int value.

rcanepa
  • 141
  • 2
  • 5
6

C++ allows multidimensional arrays to be iterated entirely through by a base-element pointer. So you can use std::fill and pass it the very first nonarray element

std::fill(a[0] + 0, a[99] + 100, 0);

In C this is formally not allowed, and you would need to iterate through each sub-array separately, because iterating beyond the first subarray's past-the-end pointer causes undefined behavior in C. Nontheless, in practice this still works.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
3

What exactly does "I can't initialize them at point of declaration" mean? "Don't know how to"? Or "can't modify the code there"?

The C++ language has a feature that initializes the entire array to 0 at the point of declaration

int a[100][100] = {};

(note, no explicit 0 is really necessary between the {}). Can you use it or not?

If you can't, then your options are: use the memset-hack, 1D-reinterpretation-hack or set each element explicitly by iterating through the array(s) (with or without the help fo std::fill).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Initialization at declaration is forbidden, for example, when `goto` or `switch` jumps over the declaration into the variable's scope. – Potatoswatter Feb 06 '17 at 02:05
3

Many answers used pointer arithmetic with fill. This can be done simpler:

int a[N][K];
fill(a[0], a[N], 0);

Basically, a[N] is a first memory address after the multi-dimensional array, no matter how many dimensions there are. This works too:

int a[N][K][L][M];
fill(**a[0], **a[N], 0);

The asterisks here dereferences the pointers down to int* type (the array brackets dereferences int**** to int***, the two asterisks does the rest of the job).

Vilius
  • 774
  • 8
  • 8
2

memset(a, 0, 100 * 200 * sizeof(int)); ought to do it.

Will A
  • 24,780
  • 5
  • 50
  • 61
1

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.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • Should also add that the semantics of static variables vs. variables with non-static declaration are very different. Variables declared as static inherently declare a state; this might not be what the OP wants. I do agree with the memset approach (or explicit initializer). – Schedler Aug 27 '10 at 18:04
0

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?

std::vector<std::vector<int> > a;
a.assign(100, std::vector<int>(200));
Mark B
  • 95,107
  • 10
  • 109
  • 188
0

I tested this solution and it worked.

int arr[100][200];

for (int i=0; i<100; i++)
    for (int j=0; j<200; j++) (arr[i][j] = 0);

for (int i=0; i<100; i++)
    for (int j=0; j<200; j++) cout << (arr[i][j]);
gmlacrosse
  • 362
  • 2
  • 8
0

Use

int a[100][200]={0};

This will initialize all the elements of the array with 0

Pranav
  • 105
  • 5
-1

Since the size is well-defined, all you need is the trailing parenthesis...

int a[100][200]();
Zak
  • 12,213
  • 21
  • 59
  • 105