gethostbyname()

This is a discussion on gethostbyname() within the C Programming forums, part of the General Programming Boards category; Please advise why the following code is not working(Segmentation fault). Merry Chirstmas! #include <stdio.h> #include <netdb.h> main() { struct hostent ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    gethostbyname()

    Please advise why the following code is not working(Segmentation fault). Merry Chirstmas!

    #include <stdio.h>
    #include <netdb.h>

    main() {

    struct hostent *h;
    char *output1;
    char *input;

    fgets(input, strlen(input), stdin);
    printf("%s", input);
    h = gethostbyname(input);
    sprintf(output1, "%u.%u.%u.%u", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned cha
    r)h->h_addr[3]);
    printf("%s", output1);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    >char *input;

    >fgets(input, strlen(input), stdin);
    // input is a char pointer,and u should allocate memory for it first

    char input[1024];

    fgets(input,sizeof(input),stdin);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gethostbyname() won't work...
    By headbr in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-14-2008, 06:51 AM
  2. GetHostByName
    By maxorator in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 01:08 PM
  3. gethostbyname allways NULL
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 08-18-2005, 01:27 AM
  4. Replies: 1
    Last Post: 09-11-2004, 08:52 AM
  5. WinSock and gethostbyname() won't return correctly...
    By SyntaxBubble in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2002, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21