c - difference between *y++ and ++*y? -
i'm confused in how code executed. suppose have
int x=30,*y,*z; y=&x; what difference between *y++ , ++*y? , output of program?
#include<stdio.h> int main(){ int x=30,*y,*z; y=&x; z=y; *y++=*z++; x++; printf("%d %d %d ",x,y,z); return 0; }
the expression x = *y++ in effects same as:
x = *y; y = y + 1; and if expression *y++; (without assignment) nothing same y++;, y start pointing next location after increment.
second expression ++*y means increment value pointed y same as: *y = *y + 1; (pointer not incremented) better clear answer first question:
suppose code is:
int x = 30, *y; int temp; y = &x; temp = *y++; //this same as: temp = *y; y = y + 1; first *y assigned temp variable; hence temp assigned 30, value of y increments 1 , start point next location after location of x (where no variable present).
next case: suppose code is:
int x = 30, *y; int temp; y = &x; temp = ++*y; //this same *y = *y + 1; temp = *y; first value of *y increments 30 31 , 31 assigned temp (note: x 31).
next part of question (read comments):
int x = 30, *y, *z; y = &x; // y ---> x , y points x z = y; // z ---> x , z points x *y++ = *z++; // *y = *z, y++, z++ , // x = x, y++, z++ x++; // increment x 31
Comments
Post a Comment