利用union做类型转换

#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;
}

 Leave a comment 

Your email address will not be published. Required fields are marked *

*


 © 2018 - Si-World