-3

I came across the following code on geeksquiz.com but could not understand how the expressions involving prefix, postfix and dereference operators are evaluated in C:

#include <stdio.h>
#include <malloc.h>
int main(void)
{
    int i;
    int *ptr = (int *) malloc(5 * sizeof(int));

    for (i=0; i<5; i++)
         *(ptr + i) = i;

    printf("%d ", *ptr++);
    printf("%d ", (*ptr)++);
    printf("%d ", *ptr);
    printf("%d ", *++ptr);
    printf("%d ", ++*ptr);
    free(ptr);
    return 0;
}

The output is given as :

0 1 2 2 3

Can somebody please explain how this is the output for the above code?

S K
  • 3
  • 5
  • That's the place to start: http://en.cppreference.com/w/cpp/language/operator_precedence (C and C++ have the same operator precedence priorities for the common set of operators). – vsoftco Aug 09 '15 at 13:56
  • [Wikipedia](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence). And that code needs to include `stdlib.h` for `malloc` and should remove the cast from `malloc`. Also, `free` the allocated memory. – Spikatrix Aug 09 '15 at 13:57
  • 1
    you should probably link the C instead of the C++ one. http://en.cppreference.com/w/c/language/operator_precedence – Alexguitar Aug 09 '15 at 13:58
  • @Alexguitar thanks, I had only the C++ bookmarked :) – vsoftco Aug 09 '15 at 13:58
  • This is going to be quite tedious to go through. Which part don't you understand? Or maybe make a simpler example. – juanchopanza Aug 09 '15 at 13:59
  • I don't understand the first, third and fourth printf results. As far as I can solve, I think the output should have been 1 1 2 3 3. – S K Aug 09 '15 at 14:01
  • Well, the only one where precedence matters is the first one, `*ptr++`. Precedence doesn't affect the others. – juanchopanza Aug 09 '15 at 14:03
  • 101 dalmatio... duplicates. – Martin James Aug 09 '15 at 14:07
  • @sksnehkumar The postfix increment / decrement operators returns the old value. So `*ptr++` actually returns `*ptr` and then does `ptr++;` – Spikatrix Aug 09 '15 at 14:15

1 Answers1

1

The precedence of operators in C can be found here.

In your example, the only expression where precedence matters is this one:

*ptr++

Here, postfix operator ++ has higher precedence, so it is equivalent to

*(ptr++)

There is no possible ambiguity in the rest ((*ptr)++, *ptr, *++ptr and ++*ptr.) You seem to be confused by the semantics of the pre- and postfix ++ operators. Bear in mind that sometimes you increment the pointer, others the thing it points to. This is what happens:

printf("%d ", *ptr++);   // Increment pointer, de-reference old value: 0
printf("%d ", (*ptr)++); // De-reference pointer, increment, yield old value
                         // Evaluates to 1, sets *ptr to 2
printf("%d ", *ptr);     // De-reference ptr, yields 2 (see above)
printf("%d ", *++ptr);   // Increment ptr, de-reference: 2
printf("%d ", ++*ptr);   // De-reference (2), increment result: 3 
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I don't understand the result of evaluation.For eg.- if the third printf statement prints 2 then for next printf, ptr is incremented first and then the value at ptr is printed because both prefix ++ and * have same precedence but right to left associativity.But supposedly that is not happening because the output is 2 again. – S K Aug 09 '15 at 14:23
  • @sksnehkumar It really has nothing to do with precedence. As I said, the only expression where that matters is the first one. Think about what happens at each step. – juanchopanza Aug 09 '15 at 14:36
  • @sksnehkumar I did a boring step by step thing. – juanchopanza Aug 09 '15 at 14:40
  • thanks, I got it.But in the fourth printf statement 2 will be printed since ptr is increment to point to 2 and then de-referenced. In next statement 2 is de-referenced and incremented to 3. Hence the output - 0 1 2 2 3 – S K Aug 09 '15 at 17:42
  • @SnehKumar Yes, sorry about that. – juanchopanza Aug 09 '15 at 19:23