0

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++;
}
devwannabe
  • 3,160
  • 8
  • 42
  • 79

2 Answers2

3

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.

Community
  • 1
  • 1
drRobertz
  • 3,490
  • 1
  • 12
  • 23
0

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.

bd2357
  • 704
  • 9
  • 17