279

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... */ };

?

4
  • 40
    char ZEROARRAY[1024] = { 0 }; Commented Apr 7, 2010 at 3:18
  • 4
    If you'll ever need to allocate memory on the heap, you can also use calloc(). For example char *zeroarray = calloc(1024, sizoef(*zeroarray)); . Commented Apr 7, 2010 at 8: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. Commented Apr 3, 2015 at 17:11
  • 1
    Possible duplicate of How to initialize an array in C Commented Oct 21, 2015 at 6:19

2 Answers 2

446

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.

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

8 Comments

{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
Please refer to: The initialized 0 is not a character. it is a integer.
{} discussion: stackoverflow.com/questions/17589533/… memset is not obviously correct: I think it only works for 0: stackoverflow.com/questions/11138188/…
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 stackoverflow.com/questions/5434865/…
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.
|
58

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.

1 Comment

Designated initializers are standard in C99. The use of ... to denote a range is a gcc-specific extension.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.