c - C 中的链表/指针

标签 c list pointers

typedef struct A A;
typedef struct A* AList;


struct A{
    char* name;
    AList *next;
};

AList create(char *name)
{
    AList new = (AList)malloc(sizeof(AList));
    new->name = name;
    new->next = NULL;
    return new;
}

void add(char* name, AList *aList)
{
    AList list = *aList;
    if (list == NULL)
    {
        list = create(name);
        *aList = list;
    }
    else
    {
        while (list->next != NULL)
            list = list->next;
        list->next = create(name);
    }
}

你好!这是我遇到问题的代码。一切正常,我只在代码的最后两行中得到“来自不兼容指针类型的赋值”,我不知道为什么。如果我更改指针,使它们具有兼容的类型(在我看来,即 list = *list->next;),我会遇到段错误。我怎样才能解决这个问题? 谢谢您的帮助, 穆科

最佳答案

改变这个

struct A{
    char* name;
    AList *next;
};

到此

struct A{
    char* name;
    AList next;
};

函数create的返回类型是AList,定义为A*。但是 A::next 的类型是 AList*,即 A**,因此类型不兼容。

关于c - C 中的链表/指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114120/

相关文章:

Python:合并字典列表

java - 如何在 java 中以相同的顺序打乱两个数组

C++ : pointer in vector and class inheritance

c - 将文件读入结构有时会无缘无故停止

c - execvp 没有读取整个 argv[]?

c++ - 使用性能分析工具的 Function Callgraph 选项时出现 Eclipse CDT 错误

algorithm - 模块化游程编码

python - 使用 Ctypes 从 Python 传递 C 结构指针

c - 为什么我不能将一个数组分配给另一个数组的子集?

c - 将指针数组传递给函数