It appears that you didn't declare your variable as a pointer to a string.
The following code seems to work fine.
int main(argc, argv)
int argc;
char *argv[];
{
struct hostent *h;
char *hostname[50];
printf("enter name of host to resolve: ");
scanf("%s", hostname);
h = gethostbyname(hostname);
if (h == NULL) {
herror("gethostbyname");
exit(1);
}
printf("IP adress: %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
}
Later,
Anthony
"Torbj
�rn S. Kristoffersen" <[EMAIL PROTECTED]> on 07/12/98 02:04:48 PM
Please respond to "Torbj�rn S. Kristoffersen" <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc: (bcc: Anthony Simon/THP)
Subject:
I'm programming a program that should use a string which contains
a domain name (f.ex. foo.foocorp.com) and printf the IP address to
stdout using gethostbyname();
This example works nice:
...
if((h=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
printf("IP adress: %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
...
But how can i replace argv with a string that has previously been
declared with
char String[25]; ?
and later use scanf or cin.
When I try replace argv with pString, the program ends up with a
Segmentation fault..
I ran strace on the program and it reported that the string contained a
newline (\n). (The newline is the error, right? correct me if i'm wrong)
How can i remove this newline? And replace it with a \0 or whatevers
necessary?
--
Torbjoern Kristoffersen <[EMAIL PROTECTED]>