I recently came across a bug in my C firmware, in which I passed the wrong type into the function. It raises a warning, but I want to understand what could happen in this scenario. During debugging, it works normally (i.e. nothing bad happens), but I want to understand if that could change depending on memory layout.
uint8_t devinfo[4] = {0}; spi_read_bytes(&devinfo, 4, ADXL362_REG_DEVID_AD);
Where that function is defined:
static void spi_read_bytes(uint8_t * data, uint8_t len, uint8_t reg)
All 4 bytes of devinfo are modified within that function.
In this, I should have passed in "devinfo" or "&devinfo[0]". But what happens if I leave it as is?
Due to the array declaration
uint8_t devinfo[4] = {0};
the expression &devinfo
has the pointer type uint8_t ( * )[4]
. But the function expects an argument of the type uint8_t *
.
static void spi_read_bytes(uint8_t * data, uint8_t len, uint8_t reg);
So the compiler issues a warning because there is no implicit conversion between the pointer types..
The code is working because the expression &devinfo
has the same value as the expression devinfo
(that is implicitly converted to the type uint8_t *
) used as an argument of the function call. The both pointers store the value of the address of the first element of the array.