Torbjørn S. Kristoffersen wrote:

> 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.. 

What is pString?

> 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)

The argument to gethostbyname shouldn't contain a trailing \n,
although this shouldn't cause it segfault.

> How can i remove this newline? And replace it with a \0  or whatevers
> necessary?

        char *p;

        for (p = String; *p; p++)
                if (*p == '\n')
                {
                        *p = '\0';
                        break;
                }

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to