C语言 引用/删除引用双指向链接列表



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

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;
}
struct INTLL_t * inner_linked_list;

struct INTLL_t是未定义的类型。它与INTLL_t无关(这是一个typedef,而不是结构(。你可能的意思是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未定义。

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

(*配置(->inner_linked_list

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

最新更新