C struct, union pointer to struct -
i have typedeffed struct , union. union contains struct , single uint32_t. goal assign value foo corresponds 'bits' in struct.
#include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef struct { uint32_t vala :1; uint32_t valb :1; uint32_t valc :1; uint32_t vald :1; uint32_t vale :1; } valstruct_type; typedef union { valstruct_type valstruct; uint32_t valuint; } valunion_type; uint32_t foo = 0; int main(void) { valstruct_type vals; vals.vala = 0x0; vals.valb = 0x1; vals.valc = 0x0; vals.vald = 0x1; vals.vale = 0x1; valstruct_type *vals_ptr; vals_ptr = &vals; foo = ((valunion_type *)vals_ptr)->valuint; return 0; } foo becomes:
decimal: 4194330 hex: 0x40001a binary: 10000000000000000011010 who can explain happening here (union pointer struct pointer, defererenced union member?)?
secondly: why bit 22 of foo set in addition bit 1,3 , 4?
bit 22 set due undefined behavior. you've created local variable never initialized, , set 5 bits of it. remaining bits whatever happen be, case uninitialized locals.
regarding first part of question... isn't clear? question (union pointer struct pointer, defererenced union member) seems answer itself. casting type doesn't have effect.
Comments
Post a Comment