Si-World

vichare的硅基世界

利用union做类型转换

September10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include 
 
using namespace std;
 
union conv {double arg; struct fl{float real; float imag;}f;};
 
int main()
{
    float farr[] = {1.0, 2.0};
    double* pd = (double*)(farr);
    conv c;
    c.arg = *pd;
    cout << c.f.real << endl;
    cout << c.f.imag << endl;
    return 0;
}