-7

Suppose you have a int pointer variable, int* q = malloc(sizeof(int))

now doing this

*q++;

q will point to some other memory location since ++ had precedence right

however doing the same in printf

printf("%d",*q++);

gives the dereference(*) operator precedence ??

Marievi
  • 4,951
  • 1
  • 16
  • 33
  • 5
    I would avoid writing code that depends on subtle language rules that you're pretty likely to forget. Is it such a loss of time to write `printf("%d", *q); q++;` ? – Mathias Dolidon Apr 01 '16 at 09:08
  • 3
    The [operator precedence](http://en.cppreference.com/w/c/language/operator_precedence) is fixed, unless there's is a very bad bug in the compiler parser (which is highly unlikely). Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? And also tell us the expected *and* actual output. And if you get compiler errors or warnings, you should show them too, complete, in full, unedited and including any possible informational notes. – Some programmer dude Apr 01 '16 at 09:10
  • @MathiasDolidon: Postfix vs. prefix is not *that* subtle, it's the whole point of postfix notation... – DevSolar Apr 01 '16 at 09:19
  • I'm not talking about prefix vs postfix, but about precedence with the star operator. You write prefix/postfix all day long, whereas depending on what you do you may not bump into `*q++` THAT often. – Mathias Dolidon Apr 01 '16 at 12:00

2 Answers2

3

++ is postfix increment. It has precedence over unary *. However, the side effect of updating the variable is done after the variable evaluation (6.5.2.4):

The value computation of the result is sequenced before the side effect of updating the stored value of the operand.

Therefore *q++ will always give you the value of *q, no matter where it is used in your code.

This is the difference between prefix and postfix increment. *++q would have incremented the pointer first, and then evaluated the value of the new address.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

There are two operators defined for incrementation - pre- and post-increment - and they both return a value! It is as if you called a function:

printf("%d", *f(q));

The preincrement operator first increments and returns the new value, the postincrement operator returns the value and increments afterwards.

OK, this is for illustration only - technically, it cannot increment after returning, of course, so it remembers the initial value, increments and returns the remembered one afterwards. This is, btw., the reason why preincrement is more efficient than postincrement, unless the compiler optimizes the differences away...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • It's probably not a good idea to use a function as analogy, because function calls in C include a sequence point. Meaning that before the function is called, all side effects will have been evaluated. Also, pre-increment is not more efficient, that's an old myth from the 1980s. (Unless you are writing C++) [See this](http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i-in-c/26482954#26482954). – Lundin Apr 01 '16 at 09:23
  • I assumed that if one asks for why *q++ results in what it results in, we are at a level where the function analogy works quite well just for understanding, whereas sequence points are much more advanced topic one would cope with later... And the question actually is tagged C++, too... – Aconcagua Apr 01 '16 at 12:14