I'm trying to get my local IP Address under MacOS.
...
this writes a random ip address 
Well, for one thing, you are not checking the return value of
gethostbyname() for failure before using the data. For another thing, this is not the best way to get local IPs anyway (even on Windows). Use
getifaddrs() instead (or
GetAdapterAddresses() on Windows).