Prannoy Mittal
Prannoy Mittal

Reputation: 1575

Conversion of uchar array to uint8 type pointer

I want to convert a array of uchar to uint8 pointer. As both are of 8 bits and value ranges from 0 to 255 so I do not think it should cause and issue.

uchar list[100];

I have to pass above list to a function which accepts pointer to uint8t. Can i pass it like this :

(uint8t *)list

such that it will not affect the value when i read it as uint8.

Upvotes: 2

Views: 1247

Answers (1)

Alexey Polonsky
Alexey Polonsky

Reputation: 1211

Well, uchar and uint8_t are not necessarily the same width ! Technically, a character can be 16bit wide. It is a rare, but it happens, especially in embedded world. So uchar may be not an 8bit type, but uint8 is always a 8bit type.

If uchar is indeed not a byte, then casting from uchar []to uint8_t * eliminates alignment restrictions. For platforms that do not support miss-aligned access (smells like embedded again!) trying to cast like this would be a bug. If you compile with GCC, add -Wcast-align flag to catch problems like this.

Upvotes: 0

Related Questions