Ref / DeRef双指向链接列表

时间:2018-06-14 23:16:34

标签: c pointers linked-list double-pointer

我传递了一个链接列表,其中包含另一个链接列表到一个函数,但是我遇到了从传递的双指针引用内部链表的问题。行push(*config->inner_linked_list...的编译器错误为'*config' is a pointer; did you mean to use '->'。内部主&config->inner_linked_list工作正常。我似乎无法弄清楚我需要在这里使用什么样的ref / deref。

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;
}

2 个答案:

答案 0 :(得分:1)

    struct INTLL_t * inner_linked_list;

struct INTLL_t是未定义的类型。它与INTLL_t无关(它是一个typedef,而不是一个struct)。你可能在这里指的是INTLL_t *struct new_inner *

    HEAD_t * config;
    config = malloc(sizeof(NODE_t));
    config = NULL;

这是内存泄漏。您刚刚丢失了malloc返回的块的唯一指针。此外,未定义NODE_t。无论如何,它应该是config = malloc(sizeof (HEAD_t))或(最好)config = malloc(sizeof *config)

BOOL functo1(HEAD_t ** config){

BOOL未定义。

    NODE_t * s = another_ll;

NODE_tanother_ll都未定义。

    push(*config->inner_linked_list, another_ll->setting,another_ll->val);

push未定义。

config是指向结构的指针。 *a->b解析为*(a->b),这需要a作为指向结构的指针,b成员也是指针(它等同于*((*a).b) })。您需要(*config)->inner_linked_list代替(或等效(**config).inner_linked_list)。

return TRUE;

TRUE未定义。

答案 1 :(得分:0)

通过指针运算符访问成员 - >比取消引用运算符*具有更高的优先级所以当你执行* config-> inner_linked_list时,它会尝试访问HEAD_t的双指针成员,这将导致错误。它在main中工作,因为配置对象是普通指针。您需要括号才能正确使用。

  

(*配置) - > inner_linked_list

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