Page 1 of 1

Suspicious pointer conversion

Posted: 08 Jan 2020 19:50
by paddywac
I'm getting a 'suspicious pointer conversion warning' for the following code; it works OK and does what I want, but why the warning?

/*
Convert int16 to bytes
----------------------
*/
void int2Bytes(int16_t parameter, uint8_t *pMSB, uint8_t *pLSB)
{
uint8_t * addr;

addr = &parameter;
*pLSB = * addr;
*pMSB = *(addr+1);
}

Then in the calling function:
uint8_t MSB, LSB;

int2Bytes(accX, &MSB, &LSB);

[accX is defined earlier in the code]

Any ideas?

Thanks
Pete

Re: Suspicious pointer conversion

Posted: 09 Jan 2020 14:52
by stefan.filipovic
Hi Pete,

You get this warning message because of the difference between the addr type and the parameter type. Just define the pointer addr as int16_t * addr;, and you will no longer get this warning message.

Kind regards,

Re: Suspicious pointer conversion

Posted: 09 Jan 2020 16:14
by paddywac
Hi, yes that makes sense.

I was using uint8_t for addr because I wanted to access the memory as a byte, but yes, it makes sense that the compiler would question it. I'll change it and see.

I was going to use a Union instead, which seems neater but maybe not as efficient.

Thanks for the support.

Pete