0

people.

I know hostent is deprecated but have to use it for a project.

For some reason if I do this...

hp = getbyhostname ("www.google.com");

hp works all right.

But then, if I do this:

char *addre;
addre = someFunction();
printf ("The address is %s", addre);
hp = gethostbyname (addre);

where someFunction() returns a char*, printf works fine and displays the address correctly, but hp returns null.

Any suggestions?

Thanks.

  • 1
    How is the memory that contains the string pointed to by `addre` allocated? Is it with `malloc()`? I suspect that `someFunction()` might be returning the address of a stack variable. Do you have all your compiler warning flags on (because the compiler can sometimes warn about errors like that)? – Celada Apr 11 '13 at 22:06
  • The problem was in someFunction() apparently. It was using strtok to do some string tokening and the delimiter I was using was "\n" . Trouble is, I was reading a HTTP GET header to take out the hostname, so I had to token it using the delimiter "\r\n" – user2272088 Apr 12 '13 at 01:04

0 Answers0