I have this array
unsigned char bit_table_[10][100];
What is the right way to fill it with 0. I tried
std::fill_n(bit_table_,sizeof(bit_table_),0x00);
but vc 2010 flags it as error.
On initialization:
unsigned char bit_table_[10][100] = {};
If it's a class member, you can initialize it in the constructor, like this:
MyClass::MyClass()
:bit_table_()
{}
Otherwise:
std::fill_n(*bit_table_,sizeof(bit_table_),0);
double doubles[10][100]; then you use the dimensions of the array for the second parameter: std::fill_n(*doubles, 10*100, 123.45);The type of bit_table_ is unsigned char [10][100], which will decay (that is, the compiler allows it to be implicitly converted to) into unsigned char (*)[100], that is, a pointer to an array of 100 unsigned chars.
std::fill_n(bit_table_, ...) is then instantiated as:
std::fill_n(unsigned char (*)[100], ...) which means it expects a value of type unsigned char [100] to initialize bit_table_ with. 0 is not convertible to that type, so the compilation fails.
Another way to think about it is that the STL functions that deal with iterators only deal with a single dimension. If you are passing in a multidimensional structure those STL functions will only deal with a single dimension.
Ultimately, you can't do this; there is no way to assign to an array type. I.e., since you can't do this:
char table[100];
char another_table[100]= { };
table= another_table;
you can't use std::fill_n on multidimensional arrays.
fill_na requirement or simply how you're attempting to solve another problem? Be careful your question doesn't suffer from the XY problem.bit_table_isn't a two-dimensional array, it's an array of pointers. If the elements ofbit_table_are supposed to point to arrays, you'll need to allocate those before initializing them. In either case, this is still a duplicate, as there are also plenty of questions about zero-initializing multidimensional arrays.memsetis generally safe for data types for which the zero bit pattern is well defined. That's the case for every builtin type (of which I am aware...), as well as every POD type (again, of which I am aware). More to the point, if you're seeing a compiler error, include the entire error (minus long file names, of course) in the question. It makes it easier for people to see why the compiler may be complaining; often the fault is not in the code that you post.