-1

My understanding of the following code is that ip is incremented in the second printf statement; then, the value pointed to by *ip is retrieved. But the output shows otherwise.

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

int main()
{
    int i[2] = { 1, 4 };
    int *ip;

    ip = i;

    printf("%d\n", *ip);
    printf("%d\n", *(ip++));
    printf("%d\n", *ip);

    return 0;
}

Output:

1
1
4

Then, by changing to the pre-increment operator, ++ip, the expected result occurred.

Code with pre-increment

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

int main()
{
    int i[2] = { 1, 4 };
    int *ip;

    ip = i;

    printf("%d\n", *ip);
    printf("%d\n", *(++ip));
    printf("%d\n", *ip);

    return 0;
}

Output:

1
4
4

My understanding of operator precedence in C is that the () have greater precedence than the * operator. With that said, why is the post-increment operator, ip++, not evaluated first - as it is within the ().

Clifford
  • 88,407
  • 13
  • 85
  • 165
hermetik
  • 115
  • 9
  • Use the rule - Just do one thing on one line of code. You will not go far wrong sticking to this – Ed Heal Jan 24 '16 at 06:53
  • 1
    It is just the way post-increment is supposed to behave, and has nothing to to with precedence. – juanchopanza Jan 24 '16 at 06:54
  • @EdHeal This was just some scratch code for testing this question I had. – hermetik Jan 24 '16 at 06:54
  • Possible duplicate of [Difference between i++ and ++i in a loop?](http://stackoverflow.com/questions/484462/difference-between-i-and-i-in-a-loop) – bolov Jan 24 '16 at 06:57
  • 2
    "_why is the post-increment operator, `ip++`, not evaluated first - as it is within the `()`._" -- It *is* evaluated first but remember that the post-fix operator returns the original value, not the updated value. – Spikatrix Jan 24 '16 at 06:58

3 Answers3

3

Regardless of evaluation order, ++ip increments before returning the new value of ip, while ip++ returns the old value of ip and then increments it. (Or, if you prefer, saves the old value, increments ip, and then returns the old value.)

That is the difference between pre- and post-increment.

In both your examples, the parentheses are redundant.

rici
  • 234,347
  • 28
  • 237
  • 341
1

My understanding of the following code is that ip is incremented in the second printf statement; then, the value pointed to by *ip is retrieved.

It's actually the other way around, see commented code on the 2nd printf: printf("%d\n", *(ip++));

int i[2] = { 1, 4 };
int *ip;

ip = i; // ip points to the first array element i[0]

printf("%d\n", *ip);        // fetch ip and printf it (i[0] = 1)
printf("%d\n", *(ip++));    // 1) fetch ip and printf it (i[0] = 1), then 2) increment ip, which now points to i[1]
printf("%d\n", *ip);        // fetch ip and printf it (i[1] = 4)
artm
  • 17,291
  • 6
  • 38
  • 54
0

First off, both pre- and post-increment operator have greater precedence than the * operator, so *ip++ is totally equivalent to *(ip++), and *++ip is also totally equivalent to *(++ip).

Then let's look at the difference between pre- and post-increment operator: The value of ++i is equal to i+1, while i++ is equal to i, although in either case, the value of i is increased by 1 after the *-increment operator is evaluated.

nalzok
  • 14,965
  • 21
  • 72
  • 139