user2211907
user2211907

Reputation:

How do I typecast an array of uint16_t into an array of uint8_t?

I have a function that does an operation on an array of uint8_t of length 32. I want to pass it an array of uint16_t of length 16, and have it do the same operation on this array, byte-by-byte.

I tried doing this:

uint8_t byteArray[32];
void function(uint16_t *inArray)
{
    byteArray = (uint8_t *) inArray;
    ... do the byte-by-byte operations on byteArray
}

but I get the error:

incompatible types when assigning to type 'uint8_t[32]' from type 'uint8_t *'

Does anyone know what I am doing wrong?

Upvotes: 0

Views: 912

Answers (2)

vonbrand
vonbrand

Reputation: 11791

An array is a collection of elements, it can't be asigned like that in C (and even less typecast). You can do it with a loop element by element, or rethink what you are doing to use the same type everywhere.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182629

uint8_t byteArray[32];

You can't assign to byteArray. It's an array, thus not modifiable in C. You could use a pointer instead or rethink what you are doing: casting in itself is a code smell and should be avoided.

Since you mention you have a function operating on this, wouldn't it be better to have the function accept a parameter than using a global object ?

Upvotes: 1

Related Questions