Search code examples
cpointerslinked-listdouble-pointer

Ref/DeRef a double pointed link list


I'm passing a linked-list, containing another linked-list to a function but I am having problems de/referencing the internal linked list from the double pointer passed. The Compiler error here for the line push(*config->inner_linked_list... is '*config' is a pointer; did you mean to use '->'. Inside main &config->inner_linked_list works fine. I cant seem to work out what sort of ref/deref I would need to use here.

typedef struct new_inner {
    wchar_t setting[10];
    wchar_t val[10];
    struct new_inner  * next;
}INTLL_t ;

typedef struct new_head {
    wchar_t name[10];
    struct INTLL_t * inner_linked_list;
    struct new_head * next;
} HEAD_t;




// In Main
int main(){
...
    HEAD_t * config;
    config = malloc(sizeof(HEAD_t));
    config = NULL;

//config populated elsewhere

    functo1(&config);
...
}


BOOL functo1(HEAD_t ** config){
    HEAD_t * current = *config;
    while(current != NULL){

    INTLL_t * s = another_ll; // Also INTLL_t
    while(s != NULL){


    push(*config->inner_linked_list, another_ll->setting,another_ll->val);
            s = s->next;
    }

    current = current->next;
}

return TRUE;
}

Solution

  • Member access through pointer operator -> has higher priority than dereference operator * so when you do *config->inner_linked_list it tries to access a member of double pointer of HEAD_t which will result in an error. It works in main because there the config object is a normal pointer. You need parentheses for correct usage.

    (*config)->inner_linked_list

    http://en.cppreference.com/w/c/language/operator_precedence