1

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.

d0rmLife
  • 4,112
  • 7
  • 24
  • 33

2 Answers2

7
*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).

ouah
  • 142,963
  • 15
  • 272
  • 331
  • +1 When you don't care to remember the order of precedence of operators, use parenthesis. The non-experts who follow you will appreciate it. In some cases, GCC will even suggest it. – Jonathon Reinhart Jan 06 '13 at 21:43
  • 1
    @JonathonReinhart I generally agree to this, but `*ptr++` is a form that any decent C programmer should be able to understand. – ouah Jan 06 '13 at 21:45
0

see the following examples:

ex: 1

int main()
{
  char *ptr = "stackoverflow";
  *ptr++;
  printf ("%s\n", ptr);//this will print "tackoverflow" i.e pointr is incremented.
  return 0;
}

ex:2

int main()
{
   char *ptr = "stackoverflow";
   *ptr = *ptr + 1;// here you are trying to assing incremented content to *ptr, but this will give you error because string literal "stackoverflow" is stored in the read only memory so you can't modify .
}
nagaradderKantesh
  • 1,672
  • 4
  • 18
  • 30