11

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

6 Answers 6

9

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.

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

1 Comment

@vittorio88: you are probably using a C++ compiler. In C a pointer to any object (m alone, of type int (*)[b]) is compatible to a pointer to void (type void *) which is what memset() expects. So no problems there.
3

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().

Comments

1

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

6 Comments

+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.
@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 - Thanks. I understood this; however, I appreciate the clarification.
@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).
@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.
|
1

Since C23 you can initialize variable length arrays (VLA) with the new intuitive empty {} initializer.

#include <string.h>

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

It can only be initialized with the empty initializer.

Comments

0

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);

1 Comment

-0.5 (rounded up) in C99 you DO can create static arrays using non-constant variables.
-2

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.

1 Comment

-0.5 (rounded up) C99 supports variable size two dimensional arrays.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.