c - postincrement pointers with arrays -



c - postincrement pointers with arrays -

cannot understand why value of a[1] not equal 10

void main() { int a[]={2,1}; int *ptr=a; *ptr++=10; //trying update a[1]=10 printf("%d",*ptr); }

could pls explain this?

let's take code, , space out some:

int main() { int a[]={2,1}; int *ptr=a; // validation assert(ptr == &a[0]); // let's *ptr++ = 10 does: *ptr = 10; ptr = ptr + 1; // validation assert(ptr == &a[1]); // printing. note printing a[1], not a[0]. printf("%d",*ptr); }

it might helpful read quote cppreference. discussing difference between postfix increment (ptr++) , prefix increment (++ptr).

the returned result of postfix version of operator original operand, side-effect beingness operand incremented/decremented after result returned.

the returned result of prefix version of operator incremented/decremented operand.

it sounds want utilize prefix increment, not postfix increment.

c pointers

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -