views:

122

answers:

5

I want to initialize a two-dimensional array of variable size to zero. I know it can be done for a fixed-sized array:

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

but it is not working if I do this:

int i = 10;
int j = 10;
int myarray[i][j] = {0};

Is there a one-line way of doing this or do I have to loop over each member of the array?

Thanks

A: 

Variable size two dimensional arrays are not supported in C. One dimension (i can't remember if it is first or second) has to be fixed. I recommend looping over it once it is defined.

erelender
-0.5 (rounded up) `C99` supports variable size two dimensional arrays.
pmg
You are right. my bad.
erelender
+1  A: 

If you get a pointer to your data structure, you could try memset.

kbrimington
+1 - memset is by far the best way to initialise arrays and new memory allocations, it certainly works in C++ so is worth a try in C.
ChrisBD
@kbrimington: In most contexts, an array name "decays" to a pointer to its first element. That works for multidimensional arrays too and, as memset() takes a `void*`, there's no issue about the type of pointer the array name by itself represents.
pmg
@pmg - Thanks. I understood this; however, I appreciate the clarification.
kbrimington
@ChrisBD: In general, `memset` is not by far the best way to initialize data. You should use it only if you have to. It's often better to initialize with `{ 0 }` in C (or with `{ }` in C++) when possible. It's more portable and less error-prone (it's easy to pass arguments to `memset` in the wrong order).
jamesdlin
@james - risk of clerical error cannot be fairly used to justify "not by far the best way"; were it so, all procedures having greater than one parameter would be "by far not the best way" to accomplish a task. :) `memset` is an efficient procedure to accomplish the OP's requirement.
kbrimington
@kbrimington: `memset` is particularly error-prone because type-checking won't catch errors where the last two arguments are swapped. (Also, I'm more accustomed to a pointer argument being followed immediately by a size argument.) Anyway, that was only my secondary argument. (And I do agree that `memset` is the right thing to use in this case.)
jamesdlin
+3  A: 

You cannot initialize it with an initializer, but you can memset() the array to 0.

#include <string.h>

int main(void) {
  int a = 13, b = 42;
  int m[a][b];
  memset(m, 0, sizeof m);
  return 0;
}

Note: this is C99. In C89 the declaration of m ( int m[a][b]; ) is an error.

pmg
A: 

You can't create a static array using non-constant variables. Try using dynamic allocation:

int i = 10;
int j = 10;
size_t nbytes = i*j*sizeof(int);
int* myarray = (int*) malloc(nbytes);
memset(myarray,0,nbytes);
vkjr
-0.5 (rounded up) in `C99` you **DO** can create static arrays using non-constant variables.
pmg
A: 

Online C99 Standard (n1256 draft), Section 6.7.8, para 3:

The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.

Emphasis mine.

As everyone else has said, your best bet is to use memset().

John Bode