Saturday 15 March 2014

function - Not sure how this output is reached in C -


I understand why the first print statement outputs -3 5 8 2 . But the second print output is 4 5 8 0 I know that 5 and 8 are because their respective values ​​remain unchanged, but I do not know How does w value 4 and z get the value of 0 I think the pointers in my The understanding can be a bit fuzzy.

  #include & lt; Stdio.h & gt; Zero mysterious (int * a, int * b, int * c) {* a = * c; * B = * b + * a; * C = * a - * b; } Int main () {int w = 5; Int x = 1; Int y = 3; Int z = 2; Mysterious (and x, and y, and y); Printf ("% d% d% d% d \ n", w, x, y, z); Mysterious (& amp; w; & amp; w; & amp; z;); Printf ("% d% d% d% d \ n", w, x, y, z); Return 0; }  

If you translate the steps inside the mysterious Do * a , * b , * c going w , w , Z comes out of it respectively:

  w = z; W = w + w; Z = w - w;  

Step through: z was 2 ; Therefore, w becomes 2 , then 4 ; And finally z becomes 4 - 4 which is 0 .


No comments:

Post a Comment