Thread: question about functions and char *

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    Question question about functions and char *

    Hello all,

    I'm having trouble with my code, hoping someone will be able to help.

    I'm trying to pass an argument to a function, have it look up an entry in a linked list, and return the corresponding entry. The problem is that when I return the pointer, it doesn't print out correctly in the calling function. Here's the two functions:

    calling function:

    Code:
    void do_something(char * src_ip, char * des_ip) {
    ...
    char * src_i_face;
    char * des_i_face;
    
    src_i_face = get_iface(src_ip);
    des_i_face = get_iface(des_ip);
    ...
    free(src_i_face);
    free(des_i_face);
    ...
    and here is the get_iface method:

    Code:
    char * get_iface(char * ip)
    {
    // pointer to top of global list
    // int_list is a global linked list
    struct bgp_cef * temp = int_list;
    char * i_face;
    char * parsed_ip;
    char * test_ip;
    
           // another method that does parsing
            parsed_ip = parse_ip(ip);
    
            i_face = (char *)malloc(50 * (sizeof(char)));
    
            while (int_list != NULL)
            {
                    // parsing
                    test_ip = parse_ip(int_list->ip);
    
                    if(!strncmp(test_ip, parsed_ip, int_list->bits)) {
                            strcpy(i_face, int_list->interface);
                            int_list = temp;
                            free(parsed_ip);
                            free(test_ip);
                            return i_face;
                    }
    
            int_list = int_list->next;
            }
    }
    obviously there's more going on in this program - but I've tried to be succinct. The problem is in the get_iface method, returning the char *.

    Any help would be greatly appreciated, as I'm ready to toss my computer out the window in frustration...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    --> ( Although i know few things ( better, nothing ) about some of your variables and function parse_ip )

    Your function get_iface will return nothing if if(!strncmp(test_ip, parsed_ip, int_list->bits)) never gets true and after the while loop is performed.

    Is that the problem?
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    um, unfortunately I don't think that's the problem...

    if I try to print i_face in the method, the variable seems fine. when I try to print it in the calling function, I get a garbled mess.

    I think this is a problem with the memory allocation, but I can't figure out what the problem is.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >if I try to print i_face in the method, the variable seems fine. when I try to print it in the calling function, I get a garbled mess.

    The calling function is the function which calls get_iface(bad english..sorry)? If yes, then it might be because you should pass src_ip and des_ip with their adress, so that the function will be called by reference.

    P.S: Are you sure that there isn't problem with the fact there might be cases where your function will retern void, instead of char* ?
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    First off, thanks to both of you for your quick responses - it's much appreciated. I'm usually a Java programmer, so going back to C after not touching it since 1rst year uni has been frustrating...

    I think the problem is in my printf call after calling the function. If I code:
    Code:
    printf("\n%s %s", str1, str2);
    it appears that str1 is being overwritten on the output screen.

    however, if I break it up:
    Code:
    printf("\n%s", str1);
    printf("\n%s", str2);
    ... everything's fine. Anyone have any ideas why this is?

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM