Index

Too few updates lately… I have been pretty busy at work. Here’s something I’ve learned today. In C, when I wanted to initialize an array to all zeros I used memset. But there’s a simpler way:

int array[3] = {0, 0, 0};

OK, that’s nice. But what if the array is really big? There’s a shorter version with the same effect:

int array[3] = {};

When you omit a parameter in an initializer, it automatically defaults to the type’s zero value. If you want to initialize an array without specifying all the elements, you can do something like that:

int array[10] = {1, [3] = 3, [8] = 2};

This will produce an array like that:

[1, 0, 0, 3, 0, 0, 0, 0, 2, 0]

See GCC’s doc about Designated Initializers.