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