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... */ };
?
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... */ };
?
If you'd like to initialize the array to values other than 0, with
gcc
you can do:This is a GNU extension of C99 Designated Initializers. In older GCC, you may need to use
-std=gnu99
to compile your code.Global variables and static variables are automatically initialized to zero. If you have simply
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:
The compiler would fill the unwritten entries with zeros. Alternatively you could use
memset
to initialize the array at program startup:That would be useful if you had changed it and wanted to reset it back to all zeros.