So, in my program, I have a variable
uint32_t buffer[16];
However, there are some times where I need to treat this as
uint8_t char_buffer[64];
The most obvious solution is a union. However, out of intellectual curiosity, is there another way to tell the compiler that I want to treat the array as an array of some arbitrary type? Something along the lines of
((uint8_t *)buffer)[i]
?
The casting approach is perfect. It will make the compiler treat the array as an array of ints. In your particluar case, use
As @JerryCoffin points out, however, there’s a probem with alignment. If you statically allocate the array statically as a
char [], it won’t necessarily be 4-byte aligned (which is needed for integers). If you surely want to avoid this problem, usemalloc()as it guarantees an appropriate alginment.