Reputation: 839
So can anybody tell me why this code compiles:
int main()
{
int CCC[1000];
std::fill(CCC, CCC + 1000, 33);
return 0;
}
and this doesn't:
int main()
{
int CCC[1000][4];
std::fill(CCC, CCC + 1000*4, 33);
return 0;
}
The compiler gives me the following error:
"incompatible types in assignment of
'const int'
to'int [4]'
"
Upvotes: 0
Views: 49
Reputation: 726609
The compiler is right: the array CCC
is composed of four-element arrays int [4]
, so you cannot assign 33
to them.
If you are on C++11, you can fix this by switching to std::array
, creating a temporary array<int,4>
, filling it with 33
s, and then filling the CCC
array with that temporary array, like this:
array<array<int,4>,1000> CCC;
array<int,4> tmp;
std::fill(tmp.begin(), tmp.end(), 33);
std::fill(CCC.begin(), CCC.end(), tmp);
Upvotes: 1
Reputation: 227418
You need to pass pointers to the beginning and one past the end of the range, i.e int*
:
std::fill(&CCC[0][0], &CCC[0][0] + 1000*4, 33);
Upvotes: 3
Reputation: 15931
What type does CCC
have? The correct answer is int **
so a pointer to a pointer pointing to an int
.
int[4]
is actually a pointer type, so your compiler expects you to pass a pointer to the initialization argument of std::fill
. But this is probably not what you want.
Upvotes: 0