C++ Undefined behaviour with unions

后端 未结 3 1713
走了就别回头了
走了就别回头了 2020-12-06 19:46

Was just reading about some anonymous structures and how it is isn\'t standard and some general use case for it is undefined behaviour...

This is the basic case:

3条回答
  •  渐次进展
    2020-12-06 20:04

    I did not get why you have used float v[2];

    The simple union for a point structure can be defined as:

    union{
    
    struct {
    
        float a;
        float b;
    };
    
    } Point;
    

    You can access the values in unioin as:

    Point.a = 10.5; 
    
    point.b = 12.2; //example
    

提交回复
热议问题