In the below program I am getting same value for different pointer operations:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
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); ---------> o/p: 2
printf("%d ", *++ptr);--------> o/p: 2
printf("%d ", ++*ptr);
}
Output: 0 1 2 2 3
My doubt is how *ptr
and *++ptr
is printing same value. It should be different as we incrementing the pointer address
The post- and pre-increment for a variable I can understand, here both are pre-increment