cast byte array pointer to int array pointer

0

I have a pointer to an array of bytes (u8). I now have a function that takes a pointer of type int (unsigned int). What is an elegant way to accomplish this without creating a new array and converting each element?

I need to convert each byte to a single int. (i.e. {0x12, 0x34, 0x56} --> {0x00000012, 0x00000034, 0x00000056})

void external_function(unsigned int *val, int length)
{
    //Write values to ...
}

void my_function(u8 *array)
{
    int length = 10;

    //Need to convert *array to (unsigned int *)
    external_function(array, length);
}
c
arrays
pointers
asked on Stack Overflow May 11, 2020 by samuelschaefer

1 Answer

1

What is an elegant way to accomplish this without creating a new array and converting each element?

There's not one. That's how you have to do it. The problem is that the 00 bytes between the elements you need just aren't there while it's a byte array.


User contributions licensed under CC BY-SA 3.0