Casting struct into int

前端 未结 3 415
长情又很酷
长情又很酷 2021-01-04 19:29

Is there a clean way of casting a struct into an uint64_t or any other int, given that struct in <= to the sizeof int? The only thing I can think of is only an \'ok\' sol

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 19:53

    you can use pointers and it will be easy for example:

    struct s {
        int a:8;
        int b:4;
        int c:4;
        int d:8;
        int e:8; }* st;
    
    st->b = 0x8;
    st->c = 1;
    int *struct_as_int = st;
    

    hope it helps

提交回复
热议问题