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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -