I'm facing a weird behavior. My C knowledge isn't enough to understand what's happening.
I'm using TI's C compiler (for CC3220SF) in Code Composer Studio. As I did usually I wrote this function:
unsigned char rxBuf[512];
void ReadID(unsigned char device[])
{
// this function reads 2 bytes and put them into
// the global buffer rxBuf;
FlashMemory_Read(ID_ADDRESS, 2);
device = &rxBuf[0];
}
(Note: here I don't pass the length because it's fixed to 2 by design).
Here how I use it:
unsigned char device[2];
ReadID(device);
as pointed out from many answers the compiler treats in the same way a parameter declared as * or [], and its value is the address (pointer) to the first element.
Two unexpected behaviors:
- the compiler warns me that in the ReadID function the 'device' variable was declared but never referenced
- debugging the code, I noticed that inside the ReadID function the device bytes point to the rxBuf values, but in the function that calls ReadID() they read always 0.
What is wrong here, and why?
The expected behavior is the device variable should point to rxBuf even when ReadID ends, because the pointer has set to the address of rxBuf. Instead it seems the address is changed only locally!