1

I am trying to increment a value in C and return the old value, and I am doing that using a pointer. The problem is that the new value is still 0 even though I am using pointer.

#include <stdio.h>
#include <stdlib.h>

int increment (int *mem) {
    int tmp;
    tmp = *mem;
    *mem++;
    return tmp;
}

int main () {
    int a = 0;
    printf("The old value of a is \t %d", increment(&a));
    printf("The new value of a is \t %d", a);
}

Now when I run this method I get the same value for a that is 0; I was expecting 1 in the second printf. I don't know what I am doing wrong here.

3
  • 8
    try (*mem)++ then read about operator precedences Jul 17, 2015 at 12:59
  • 1
    You really should learn how to use the debugger, and enable all warnings & debug info in your compiler, and read more about C syntax. Don't write some syntax that you don't master (so add parenthesis as much as you want) Jul 17, 2015 at 13:01
  • 2
    BTW, I'm coding in C since the 1980s and I never code *mem++ because I find it so unreadable (so I code either *(mem++) or (*mem)++ or even mem[0]++....), even if it has some defined meaning Jul 17, 2015 at 13:17

3 Answers 3

9

Change this

*mem++;

to this

(*mem)++;

The problem lies in the operators priority. You may want to read about C Operator precedence.


So, what does your code do? It increments the value of the pointer, since ++ operator is activated first and then * gets activated, having no real effect.

As a result, your code invokes undefined behavior, since you access (and eventually write) into an invalid memory location since the pointer is incremented before the value is written to it.

5
  • Meant to use the C version. I deleted it. Didn't notice your link.
    – csnate
    Jul 17, 2015 at 13:06
  • 1
    IMO, you should mention that the OP's code invokes UB.
    – Spikatrix
    Jul 17, 2015 at 13:06
  • Good idea @CoolGuy, since (s)he modifies the value of the pointer and the access it, right? That's what you mean or you had something else in mind? It's ok csnate.
    – gsamaras
    Jul 17, 2015 at 13:07
  • 1
    Yes. The OP's code writes into an invalid memory location since the pointer is incremented before the value is written to it.
    – Spikatrix
    Jul 17, 2015 at 13:09
  • Bravo @CoolGuy, that really improved the answer, thank you. If you want something else changed in the answer, let me know.
    – gsamaras
    Jul 17, 2015 at 13:11
5

Maybe you missed some parentheses?

#include <stdio.h>
#include <stdlib.h>

int increment (int *mem) {
    int tmp;
    tmp = *mem;
    (*mem)++; // problem was here.
    return tmp;
}

int main (){
    int a = 0;
    printf("The old value of a is \t %d", increment(&a));
    printf("The new value of a is \t %d", a);
}
1
  • 1
    Parentheses should be used for plural. Since I am Greek, I had to tell! :P By the way, +1 for the complete code.
    – gsamaras
    Jul 17, 2015 at 13:12
0

In addition to what everyone else has posted about operator precedence, if you are passing in a pointer to an int to be incremented there is no reason to return a copy of the int via tmp. You will have access to the value in mem outside of the function.

#include <stdio.h>
#include <stdlib.h>

void increment (int *mem) {
    (*mem)++;
}

int main () {
    int a = 0;
    printf("The old value of a is \t %d", a);
    increment(&a);
    printf("The new value of a is \t %d", a);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.