I personally don't recommend memset
for general initialization. There are too many things you have to be sure of to be able to correctly and portably use it. It's basically only good for char
arrays or microcontroller code.
The way to initialize an array in C is the following (assume size N
):
for (i = 0; i < N; ++i)
buff[i] = init_value;
With C99, you can do more interesting stuff. For example, imagine the buffer is an array of the following struct:
struct something
{
size_t id;
int other_value;
};
Then you can initialize like this:
for (i = 0; i < N; ++i)
buff[i] = (struct something){
.id = i
};
This is called designated initializer. Like all other cases of initializers, if you don't mention a specific field of the struct, it will be automatically zero initialized.