Andreas
Andreas

Reputation: 7550

How to convert uint16_t[2N] to uint32_t[N] effectively?

I've got an array of six uint16_t which is actually three uint32_t, with the bits in the right order and all. How can I cast the former to the latter as effectively as possible?

The number of elements in the array is known at compile-time.

Upvotes: 4

Views: 2824

Answers (1)

David Heffernan
David Heffernan

Reputation: 613342

Like this perhaps:

uint16_t arr16[6];
uint32_t *parr32 = (uint32_t*)(&arr16);

And now you can use parr32[i] to refer to elements of the overlayed arr16 array.

Upvotes: 12

Related Questions