Text in Yellow shows current ptr position.
Even though the array is dynamically allocated let me name it as A[5]={ , , , , } //Initially
Loop line for(..) { *(p+i)=i }
Array will look like A={0,1,2,3,4}
Before printing output points to be noted:-
- Precedence of post (++/--) > dereference (*) >= pre(++/--)
- when precedence is same like in case of dereference (*) and pre(++/--) Rigth to left associativity is used.
- printf(*ptr++)
- it will be parentheses as *(p++) but, post-increment effect comes in the picture, so it will dereference current position address value and then increase the pointer.
- OUTPUT 0
- A={0,1,2,3,4}
- printf( (*ptr)++) )
- easy one, dereference the value and increment the value.
- OUTPUT = 1
- but due to post increment side effect value will be updated after print.
- A={0,2,2,3,4,}
- don't know what does that mean.
- print( *++ptr )
- precedence same but Right to left associativity so it will be treated as *(++ptr)
- increment pointer then dereference it.
- OUTPUT = 2
- A={0,2,2,3,4}
- print(++*ptr)
- precedence same but Right to left associativity so it will be treated as ++(*ptr)
- dereference first then increment value.
- OUTPUT = 3
- A={0,2,3,3,4}