c - Why this code does what it does? -
there's recent question posted. here's link:
output sorted in weird way
i know how it's wrong, i'm trying figure out is, why produces it's output way is?
after changing getch()
getchar()
compiled , run few times. changes first 4 letters
. can't see where
change actuallly takes place in code.
appreciated.
this happens in swap
function:
//swapping function void swap(char **first, char **second) ...
the op assumed 'char *' integral type, , moving around move the string around. second part swap function should accept pointers data, not actual data. swaps addresses of data , not touch data itself.
however, when called char * *
, goes wrong. swaps data @ address of 'a pointer to'. input not "pointer to-pointer to".
'pointer to' has sizeof int
on systems (where 'most' subjective assessment -- search "sizeof pointer" discussions , opinions). routine swaps not string or pointer-to-string, swaps integers @ target addresses. since integer is, according result, 4 bytes long on op's machine, code "swaps" first 4 bytes of target strings.
Comments
Post a Comment