I haven't found any solution among the many many threads about this. My exact problem is:
I have an array of integers such as unsigned int arr[2] = {0xFEBD1213, 0x1213FEBD};
I would like to access those integers char by char, meaning that I need to read : 0x13, 0x12, 0xBD, 0xFE, 0xBD, 0xFE, 0x13, 0x12.
I tried many, many things and I did not succeeded yet.
Note : I would also like to do the opposite : having a char array with a size such as
size %4 == 0, and reading it as an integer array. E.g :unsigned char arr[8] = {0x13, 0x12, 0xBD, 0xFE, 0xBD, 0xFE, 0x13, 0x12}and read0xFEBD1213, 0x1213FEBD;
Is there any way of doing such a thing?
Minimal reproducible example:
#include <stdio.h>
#include <stdlib.h>
void main(void){
unsigned int arr[2] = {0xFEBD1213, 0x1213FEBD};
unsigned char * ptr;
ptr = *&arr; // I need a variable. Printing it doesn't matter to me. I am aware that there are easy solutions to print the right values there.
for(int i = 0; i < 2 * 4; i++){
printf("%x\n", *ptr);
ptr = (ptr++);
}
}
(I am aware that there are many cleaner way to code this, but I don't have the control over the type of the given array)
ptr = (ptr + i);==>ptr++;