c++ - Pointer indirection issue -
this program produces 0 1 1 output against expected output 0 1 2. can explain why increment operator doesn't work prefix?
#include <stdio.h> int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); (i=0; i<5; i++) *(ptr + i) = i; printf("%d ", (*ptr)++); printf("%d ", *ptr); printf("%d ", *++ptr); return 0; }
assume int *p = ptr
:
printf("%d ", (*ptr)++); // print ptr[0] increment ptr[0] ==> 0 printf("%d ", *ptr); // print ptr[0] ==> 1 printf("%d ", *++ptr); // increment ptr print p[1] ==> 1
Comments
Post a Comment