2
#include <stdio.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++);//prin1
    printf("%d ", (*ptr)++);//print2
    printf("%d ", *ptr);//print3
    printf("%d ", *++ptr);//print4
    printf("%d ", ++*ptr);//print5
}

i am getting

0 1 2 2 3

as output.

I am not able to understand why "print4" is printing 2 it should print 3 as pointer *++ptr will be treated as *(++ptr) i.e pointer will be incremented first

4
  • 2
    In C there is no need to cast the return value of malloc() Commented Apr 9, 2014 at 19:41
  • 3
    If you ever work on a project with someone else, do them a favor and never write code like this. Commented Apr 9, 2014 at 19:45
  • As haccks points out, some of your code is incrementing the values at the pointer location, other bits of your code is incrementing the pointer itself. Commented Apr 9, 2014 at 19:51
  • 1
    Please remove all casts where you don't know exactly what you are doing and can explain why the cast is neccessary for correct working. Never muzzle the compiler, instead ask it to speak up: Use -Wall -Wextra, and handle all warnings appropriately. (You need to add #include <stdlib.h>) Commented Apr 9, 2014 at 19:51

4 Answers 4

5

*ptr++ --> Print 0, increment ptr by 1.
(*ptr)++ --> Print 1, increment 1 to 2.
*ptr --> Print the incremented value stored, i.e 2.
*++ptr --> Increment the ptr by 1 and then print the value pointed by ptr, which is 2.
++*ptr --> Increment the value , i.e 2, pointed by ptr and then print it, i.e 3.

Sign up to request clarification or add additional context in comments.

2 Comments

in print3 i am using pre increment so it should print 3
That preincrement will modify ptr by 1 not the content of ptr points to. (I hope you talked about print4).
3

In C, it is better to not cast the result of malloc(). Doing so can hide bugs that the compiler would be able to warn you about, and the bugs in the worst case can cause your program to crash.

    int *ptr = malloc(5 * sizeof(int));

ptr now points to memory for 5 ints, and you initialize them with sequential values in your loop. At then end of the loop, the situation can be visualized like this:

   +---+---+---+---+---+
   | 0 | 1 | 2 | 3 | 4 |
   +---+---+---+---+---+
     ^
    ptr

Now, we can draw a picture to correspond to each line of code.

printf("%d ", *ptr++);//prin1       +---+---+---+---+---+
                                    | 0 | 1 | 2 | 3 | 4 |
                                    +---+---+---+---+---+
                                          ^
                                         ptr

Since post increment is used, 0 is printed, and ptr is incremented.

printf("%d ", (*ptr)++);//print2    +---+---+---+---+---+
                                    | 0 | 2 | 2 | 3 | 4 |
                                    +---+---+---+---+---+
                                          ^
                                         ptr

The dereferenced value 1 is printed, and then post-incremented, so it becomes 2. ptr did not move.

printf("%d ", *ptr);//print3        +---+---+---+---+---+
                                    | 0 | 2 | 2 | 3 | 4 |
                                    +---+---+---+---+---+
                                          ^
                                         ptr

Now, the dereferenced value 2 is printed, and nothing was changed.

printf("%d ", *++ptr);//print4      +---+---+---+---+---+
                                    | 0 | 2 | 2 | 3 | 4 |
                                    +---+---+---+---+---+
                                              ^
                                             ptr

The pre-increment on ptr moves it to the next element, and the dereferenced value 2 is printed.

printf("%d ", ++*ptr);//print5      +---+---+---+---+---+
                                    | 0 | 2 | 3 | 3 | 4 |
                                    +---+---+---+---+---+
                                              ^
                                             ptr

The dereferenced value 2 is pre-incremented, so the value 3 is printed.

Comments

0

That is because you didn't increase at print3.

1 Comment

Not enough explanation.
0

You are probably not understanding where ptr is pointing to in the flow... does the below extra comments clear it up?

printf("%d ", *ptr++);  // ptr now point to ptr[1] which is initialized to 1
printf("%d ", (*ptr)++);// update ptr[1] to 1+1 = 2
printf("%d ", *ptr);    //  printing the value pf ptr[1]
printf("%d ", *++ptr);  // incrementing ptr, to pointing to ptr[2] which 
                        // is initialized to 2....
printf("%d ", ++*ptr);  // incrementing the value of ptr[2] to (2+1) = 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.