c: when using a pointer as input in a function incrementing the pointers value by using *pointer++ doesn't work -
while learning c (i new it), playing around pointers. here can see code:
#include <stdio.h> void change(int *i) { *i += 1; } int main() { int num = 3; printf("%d\n", num); change(&num); printf("%d\n", num); return 0; }
my aim replace incrementing num value without reassigning so:
num = change(num);
that's why passing memory location of num
using &
: used pointer. before version in code same. thing different said *i++;
instead of saying *i += 1;
now question why can't *i++
?
now question why can't *i++
due operator precedence, *i++
same *(i++)
.
*(i++);
is equivalent to:
int* temp = i; // store old pointer in temporary variable. i++; // increment pointer *temp; // dereference old pointer value, noop.
that not want. need use (*i)++
or ++(*i)
. these dereference pointer first , increment value of object pointer points to.
Comments
Post a Comment