0

Picture this scenario. I need to store the quantity of products of a delivery. Each delivery and product has a number to identify it. I am using an array somewhat like this: int array[500][500];

The main issue is that I do not have idea how to initialize the whole array with 0s. Any left-out junk in the 500 slots can backfire eventually in the many other algorithms I still have to code. I used to do int array[500] = {0}; but in this case, there are two ID's related to each index.

15
  • 1
    memset(buf, 0, sizeof buf); for an array named buf. Commented Apr 2, 2020 at 19:54
  • 6
    int array[500][500]={{0}}; should work, I believe Commented Apr 2, 2020 at 19:55
  • 3
    @wildplasser That will zero the array, but it wouldn't be initialization. Commented Apr 2, 2020 at 19:58
  • 1
    Related: stackoverflow.com/q/3718716/1025391 Commented Apr 2, 2020 at 20:00
  • @ChristianGibbons: Is that some pedantic vocabulary intricacy? If it zeros the array, it has the same effect as if it had been "initialized." Commented Apr 2, 2020 at 23:06

4 Answers 4

7

I used to do int array[500] = {0}; but in this case there is two ID's related to each index.

Doesn't matter; that's still valid and appropriate in C11.

Try it and see!

Live demo

(Don't mind the ".cpp"; that's fixed for us by Coliru, but -x c tells GCC to compile the code as C.)


You do not need extra brackets. The standard spells this out explicitly:

[C11: 6.7.9/20]: If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the contained union. Otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.

And later provides an example:

[C11: 6.7.9/26]: EXAMPLE 3 The declaration

int y[4][3] = {
   { 1, 3, 5 },
   { 2, 4, 6 },
   { 3, 5, 7 },
};

is a definition with a fully bracketed initialization: 1, 3, and 5 initialize the first row of y (the array object y[0]), namely y[0][0], y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] and y[2]. The initializer ends early, so y[3] is initialized with zeros. Precisely the same effect could have been achieved by

int y[4][3] = {
   1, 3, 5, 2, 4, 6, 3, 5, 7
};

The initializer for y[0] does not begin with a left brace, so three items from the list are used. Likewise the next three are taken successively for y[1] and y[2].

Recall, by the way, that arrays are aggregates in C:

[C11: 6.2.5/21]: Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types.

Now, for the case of providing only one value (the 0), the normal rule you're used to (that all remaining elements take on zero anyway) still applies:

[C11: 6.7.9/21]: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

You may still receive style warnings from your compiler about this (I don't), but you can ignore them.

If you're using an antique version of C, though, you may need to double-up on your braces; I'm not too familiar with the history of this feature.

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

13 Comments

Nops: warning: missing braces around initializer as @ChristianGibbons is pointing in comments the correct way is int array[500][500]={{0}}; (anyway I'm not the not the downvoter)
@DavidRanieri I don't get that warning, and I don't see why one should get it, as the code is perfectly valid. See e.g. 6.7.9/26 in open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf (roughly C11). You don't need more braces; a flat sequence of initial values works just fine for multi-dimensional arrays.
int array[500][500] = { 0 }; works fine with C11 (gcc -std=c11 -O0 -g3 -pedantic -Wall -Wextra -Wconversion -c -fmessage-length=0 -MMD -MP -MF"my.d" -MT"my.o" -o "my.o" "../my.c")
@AsteroidsWithWings you are totally right! get my upvote, another false positive of gcc? :(
Some versions of GCC have generated the 'missing braces' warning; others don't. Generally, the = { 0 }; notation is allowed without warning (in modern GCC, say version 5 and later) — it's the 'universal initializer', valid for scalar integers as well as arrays and structures and arrays of structures and ... You can probably ensure the 'missing braces' warning with an explicit -W flag — it may or may not be part of -Wall or -Wextra these days.
|
4

If the sizes of the array are compile-time constants like the value 500 then it is enough to write

int array[500][500] = { 0 };

Or you can even use a designated initializer for example like

int a[500][500] = { [0][0] = 0 };

or like

int a[500][500] = { [0] = { 0 } };

or something similar.

In this case all elements of the array will be zero-initialized.

If it is a variable length array then you may not initialize it at declaration. In this case you can use the standard C function memset. For example

#include <string.h>

//...

int main( void )
{
    size_t m = 500;
    size_t n = 500;

    int array[m][n];

    memset( array, 0, m * n * sizeof( int ) );
    //...

1 Comment

In your example, memset( array, 0, m * n * sizeof( int ) ); can be simplified as memset(array, 0, sizeof array); This is one of the few examples of sizeof being computed at runtime.
0

Use memset to initialize the entire memory block to 0:

NAME         
   memset - fill memory with a constant byte

SYNOPSIS         
   #include <string.h>

   void *memset(void *s, int c, size_t n);

DESCRIPTION         
   The memset() function fills the first n bytes of the memory area
   pointed to by s with the constant byte c.

RETURN VALUE         
   The memset() function returns a pointer to the memory area s.

So for your example:

int array[500][500];
memset(&array, 0, sizeof(int) * 500 * 500);

4 Comments

This is not initialization.
C has a specifics about initialization. It is something done at the definition of the variable. What is here is a way to assign the data.
Alternate to memset(&array, 0, sizeof(int) * 500 * 500); --> memset(array, 0, sizeof array);
Remove the & from memset(&array, 0, sizeof(int) * 500 * 500). This expression can also be simplified as memset(array, 0, sizeof array)
0

If your array is defined as a global variable or a static variable local to a function, you don't actually need to initialize it, all elements will have a zero value at program start.

If the array is defined inside a function body with automatic storage, int array[500][500];, you can initialize it at the point of definition with

int array[500][500] = { 0 };

If you mean to reset all values to zero at any point during program execution, you can use memset from <string.h>:

memset(array, 0, sizeof array);

Comments