Search code examples
cpointerscastingstructunions

C struct, union pointer to struct


I have a typedeffed struct and an union. The union contains the struct and a single uint32_t. The goal is to assign a value to foo that corresponds to the 'bits' in the 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 what is happening here exactly (union pointer to a struct pointer, defererenced to a union member?)?

Secondly: why is bit 22 of foo set in addition to bit 1,3 and 4?


Solution

  • Bit 22 is set due to undefined behavior. You've created a local variable that you never fully initialized, and you then set 5 bits of it. The remaining bits are whatever they happen to be, which is the case with uninitialized locals.

    Regarding the first part of your question... what isn't clear? Your question (union pointer to a struct pointer, defererenced to a union member) seems to answer itself. Casting something to the type it already is doesn't have any effect.