0

main function:

int main()
{
    if((ban_file = open_file("banned_ips.txt")) == NULL)
        goto exit;

    ban_lst = NULL;
    cpy_to_list(ban_file, ban_lst);
    close_file(ban_file);
    dealloc_list(ban_lst);
exit:
    return 0;
}

cpy_to_list function:

void cpy_to_list(FILE *file, LINKED_LIST *lst)
{
    char *line = malloc(1024);

    while((line = fgets(line, 1024, file)) != NULL)
    {
        add_node(line, lst);
    }

    free(line);
}

add_node, dealloc_list & create_list:

LINKED_LIST *create_list(void)
{
    LINKED_LIST *tmp;

    if((tmp = malloc(sizeof(LINKED_LIST))) == NULL)
        perror("Error during memory allocation");

    return tmp;
}

void add_node(const char *str, LINKED_LIST *lst)
{
    struct list_node *tmp_node;

    tmp_node = create_list();
    tmp_node->str = str;

    if(lst != NULL)
    {
        tmp_node->next = lst;
        lst = tmp_node;
    }
    else
    {
        lst = tmp_node;
        lst->next = NULL;
    }
}

void dealloc_list(LINKED_LIST *ptr)
{
    free(ptr);
}

How come ban_lst is a NULL pointer?

2 Answers 2

3
cpy_to_list(ban_file, ban_lst);

Whatever it does, this function cannot change what ban_lst points to. Whatever it does, at the end of the call ban_lst will point to where it pointed before. Perhaps you want cpy_to_list to accept a LINKED_LIST ** ?

Your add_node function expresses the problem even better.

lst = tmp_node;

This line only changes the functions idea of the pointer. It doesn't change anything for the caller.

This C FAQ touches on the subject.

Sign up to request clarification or add additional context in comments.

Comments

3

C is a pass-by-value language. You pass ban_lst to cpy_to_list(), but it can't change it from the caller's point of view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.