Thread: Segfault and Warning help

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Segfault and Warning help

    Hey guys,

    No matter what I do, I can't get this code to stop segfaulting, and as you can see by the debugging printf's, it does so at the assignment of `in' .. there's another warning on compilation:

    Code:
    tmp.c:25: warning: assignment makes pointer from integer without a cast
    .. this quick code was written to re-create the segfault (the program I'm writing is quite long, so I decided instead of pasting all of it, or a snipped portion of it, i'd write a quick example to show you what happens), so you could all see when in the execution the segfault occurs, and hopefully offer up some suggestions as you how I could fix this =\ .. anyway, any ideas would be greatly appreciated! Thanks =]

    Here's the code:
    Code:
    #include <stdio.h>
    #include <netdb.h>
    #include <netinet/in.h>
    
    int resolve(char *, struct hostent *, struct in_addr **);
    
    int main(int argc, char *argv[])
    {
        struct hostent *hent;
        struct in_addr **in;
    
        printf("DEBUG: Defined structures.\n");
        in = (struct in_addr **)hent->h_addr_list;
        printf("DEBUG: Passed `in' initialization.\n");
        
        resolve(argv[1], hent, in);
        
    return 0;
    }
    int resolve(char *host, struct hostent *hent, struct in_addr **in)
    {
    char *addr;
    
        addr = inet_ntoa(**in);
        
        if((hent = gethostbyname(host)) == NULL) {
            fprintf(stderr, "Error: gethostbyname() failed\n");
            return -1;
        }
        
        printf("%s\t\t%s\n", host, addr[0]);
        
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Do you want us to guess which is line 25?

    > in = (struct in_addr **)hent->h_addr_list;
    Lemme guess, where is hent pointing?

    You have
    struct hostent *hent;

    Then a printf

    Then BOOM!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 10:04 PM
  2. Reading from an data?
    By Tarento in forum C Programming
    Replies: 11
    Last Post: 06-07-2006, 01:50 AM
  3. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM