255

I need a big null array in C as a global. Is there any way to do this besides typing out

char ZEROARRAY[1024] = {0, 0, 0, /* ... 1021 more times... */ };

?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • 36
    char ZEROARRAY[1024] = { 0 }; –  Apr 07 '10 at 03:18
  • 3
    If you'll ever need to allocate memory on the heap, you can also use calloc(). For example char *zeroarray = calloc(1024, sizoef(*zeroarray)); . – Andrei Ciobanu Apr 07 '10 at 08:10
  • 3
    N.B. calloc is fine for `char` etc, but if you want an array-of-pointers, you should set them explicitly to NULL, there is (absurdly!) no guarantee that NULL is represented as zero-bytes. This even though the literal `0` implicitly represents the null pointer. – Adrian Ratnapala Apr 03 '15 at 17:11
  • 1
    Possible duplicate of [How to initialize an array in C](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c) – AechoLiu Oct 21 '15 at 06:19

2 Answers2

412

Global variables and static variables are automatically initialized to zero. If you have simply

char ZEROARRAY[1024];

at global scope it will be all zeros at runtime. But actually there is a shorthand syntax if you had a local array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. You could write:

char ZEROARRAY[1024] = {0};

The compiler would fill the unwritten entries with zeros. Alternatively you could use memset to initialize the array at program startup:

memset(ZEROARRAY, 0, 1024);

That would be useful if you had changed it and wanted to reset it back to all zeros.

wovano
  • 4,543
  • 5
  • 22
  • 49
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 7
    {0}; works fine, C99 [$6.7.8/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 – Саша Зезюлинский Jan 25 '16 at 11:10
  • 1
    Please refer to: The initialized 0 is not a `character`. it is a `integer`. – Yonggoo Noh May 04 '16 at 04:43
  • 1
    `{}` discussion: http://stackoverflow.com/questions/17589533/is-an-empty-initializer-list-valid-c-code `memset` is not obviously correct: I think it only works for 0: http://stackoverflow.com/questions/11138188/set-all-bytes-of-int-to-unsigned-char0-guaranteed-to-represent-zero – Ciro Santilli OurBigBook.com May 10 '16 at 18:54
  • 2
    If it's an array of structs, and using `-Werror=missing-braces` in gcc, it must be initialized to `{{0}}`. If the first struct element is an other struct then`{{{0}}}` and so on. See https://stackoverflow.com/questions/5434865/quickest-way-to-initialize-an-array-of-structures-to-all-0s – Tor Klingberg Aug 25 '17 at 15:41
  • 3
    Today I encountered the weird ... `int arr[256]={1,2,7,{0}};` ... which brought me here. Didn't even know this partial-zeroing was a thing until I saw it. – Neil Gatenby Jul 11 '19 at 08:26
  • Note that char ZEROARRAY[1024] = {0}; initializes each element with character NULL of ASCII table not character 0. If you print ascii value of any of the elements of this array it will print 0(ascii value of character NULL) not 48 (ascii value for character 0) – Ayush Nov 02 '21 at 08:29
  • Suggest cautioning against creating large arrays on the stack; 1024 is "borderline-large". – einpoklum Jul 31 '22 at 19:34
  • I don't like the - char ZEROARRAY[1024]; - because non global arrays are not initialised and doing that can make you forget about that detail and get errors on arrays created later on. – Yellow Aug 14 '22 at 22:43
52

If you'd like to initialize the array to values other than 0, with gcc you can do:

int array[1024] = { [ 0 ... 1023 ] = -1 };

This is a GNU extension of C99 Designated Initializers. In older GCC, you may need to use -std=gnu99 to compile your code.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Deqing
  • 14,098
  • 15
  • 84
  • 131