The following code will get you a char*
view of all the float
s in your input
parameter.
However, I'd very careful that this is actually what you want. The char* array is not endian robust, and I always try to avoid handing allocated pointers back to users. Anyone using this function will need to deallocate output
with delete[]
, but that is in no way obvious from the function name or signature, a recipe for fragile code.
void foo(const std::vector<std::vector<float> > &input, char* &output )
{
//this was likely an error, it is always an array of size 4
//char charBuf[sizeof(output)];
std::vector<float> tmp_output;
int counter = 0; // Why was this here?
for(unsigned int i=0; i<input.size(); i++)
{
// This is slightly more efficient than the hand rolled loop below.
// std::copy( input[i].begin(), input[i].end(),
// std::back_inserter<float>(tmp_output) );
for(unsigned int p=0; p<input.at(i).size(); p++)
{
tmp_output.push_back(input.at(i).at(p));
}
}
output = new char[tmp_output.size()*sizeof(float)];
std::copy( reinterpret_cast<const char*>(&(*tmp_output.begin())),
reinterpret_cast<const char*>(&(*tmp_output.end())),
output );
}