Not sure what I'm doing wrong because aValue and bValue aren't being updated.
int aValue;
int bValue;
void setup() {
aValue = 0;
bValue = 0;
}
void loop() {
someFunc(&aValue, &bValue);
// code for printing aValue and bValue
}
void someFunc(int *a, int *b) {
*a++;
*b++;
}
The problem is that pointers and post-increment does not do what you want.
If you write
void someFunc(int *a, int *b) {
*a = *a+1;
*b = *b+1;
}
it works
See ++ on a dereferenced pointer in C? for an explanation of why *a++
increments the pointer itself.
The variables a and b in someFunc are copies and you are incrementing the copy. Precedence order for post increment is higher then the pointer de-reference so you are incrementing the copies of your pointers. The de-reference has no effect.
When in doubt use parentheses.
void someFunc(int *a, int *b)
{
(*a)++;
(*b)++;
}
although some people say you shoud do
void someFunc(int *a, int *b)
{
++(*a);
++(*b);
}
Since post increment technically returns a value while pre increment just increments. In this case most compilers would produce the same code. I have never looked at the AVR compiler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.