Search code examples
cpointersmemoryupdating

C: updating memory address contents using pointers with ++ / --


I was wondering, why does:

*some_var++;

not do the same as:

*some_var = *some_var + 1;

... is it because in the second example the <*> dereferencing operator is being used for two distinct purposes?

*some_var = *some_var + 1;

Which is to say: the first instance of *some_var is setting the contents of &some_var whereas the second instance of *some_var is calling the current contents of &some_var? ...That being a distinction C cannot make with the statement: *some_var++;?

Furthermore, does:

*some_var++;

do anything, and if so, what?!

Thanks for any input... perhaps a trivial matter but I am curious nonetheless.


Solution

  • *some_var++;
    

    is equivalent to

    *(some_var++);
    

    and not equivalent to:

    (*some_var)++;
    

    ++ postfix operator has higher precedence than * unary operator.

    By the way, as you don't use the value of the * operator in your statement, *some_var++; statement is also equivalent to some_var++; (assuming some_var is not a pointer to a volatile object).