I have a simple function that takes a pointer to a variable & increments the value stored at the variable's address.
Using MPLAB X as my IDE on a PIC24FJ256 device.
//******** main.c *********//
// Declared these globally so MPLABX debugger can see it
uint8_t testCount;
uint8_t *testCount_p = &testCount;
void incrCount( uint8_t *counter )
{
*counter += 1; // This line increments by 5 instead of 1
//*counter++; // This line increments by 5 instead of 1
//(*counter) += 1; // This line increments by 3 instead of 1
}
void main ( void )
{
testCount = 0x00; // Initialize the count to = 0
incrCount( testCount_p );
// At this point, testCount = 0x05 ???
}
I am not sure why, when I dereference & increment the local pointer inside incrCount, the global variable testCount is incremented by 5? If I enclose the increment operation in parentheses like this:
//(*counter) += 1; // This line increments by 3 instead of 1
testCount is incremented by
3 each time.
Does anybody here have an idea of why this is so? I'm expecting the global variable testCount to increment by 1 each time incrCount() is called, but getting increments of 5 and 3 instead.