C中引用指针的等效?

时间:2017-05-11 17:17:29

标签: c

我创建了一个链表,以及一个返回迭代器的函数。将迭代器分配给另一个指针并释放指针时,它什么都不做,因为它不是引用。如何删除列表中的项目?

示例:

//initialize struct list, insert 1 struct (this is only done if list is empty)
some_struct* struct_list;
struct_list = (some_struct*)malloc(sizeof(some_struct));
strncpy(struct_list->name, "foo", 4);
struct_list->name[3] = '\0';

typedef struct some_struct
{
    char name[MAXLENGTH];
    struct some_struct* next;
} some_struct;

some_struct* find_struct(char* name)
{
    char* iter;
    for (iter = struct_list; iter != NULL; iter = iter->next)
    {
        if (strcmp(name, iter->name) == 0)
            return iter;
    }
    return NULL;
}
some_struct* name;
name = find_struct("foo");  //since there's only 1 struct, name == struct_list
free(name);  //struct_list still contains 1 struct with name "foo"

我确保链接列表正常工作,它确实找到了名称,但这并没有做任何事情

编辑:为了澄清,取消链接/链接确实摆脱了结构。但是,如果结构中只有1个项目,那么这不起作用

0 个答案:

没有答案