C++ memcpy from double array to float array

dmessf picture dmessf · Jun 2, 2010 · Viewed 11k times · Source

Is is possible to memcpy from a double array to a float array safely?

Answer

fredoverflow picture fredoverflow · Jun 2, 2010

Depends on what you want. The values certainly won't be preserved. If you need that, use std::copy.

#include <algorithm>

int main()
{
    double a[] = {1.618, 3.1416, 2.7, 0.707, 1.0};
    float b[5];
    std::copy(a, a + 5, b);
}