I have the following code:
main()
{
uint8_t readCount;
readCount=0;
countinfunc(&readCount);
}
countinfunc(uint8_t *readCount)
{
uint8_t i;
i = readCount;
....
}
Problem is that when it enters in the function the variable i has a different value then 0 after the assignment.
It's because in countinfunc the variable is a pointer. You have to use the pointer dereference operator to access it in the function:
i = *readCount;
The only reason to pass a variable as a reference to a function is if it's either some big data that may be expensive to copy, or when you want to set it's value inside the function to it keeps the value when leaving the function.
If you want to set the value, you use the dereferencing operator again:
*readCount = someValue;
countinfunc(uint8_t *readCount)
{
uint8_t i;
i = *readCount;
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With