Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-分段错误中的链表_C_Linked List_Segmentation Fault - Fatal编程技术网

C-分段错误中的链表

C-分段错误中的链表,c,linked-list,segmentation-fault,C,Linked List,Segmentation Fault,我是一个初学者,正在学习如何用C语言制作链表。每当我试图打印链表时,链表打印出来的效果都很好,但最后总是出现分段错误 当我使用GDB backtrace时,它指向行->条目=*((*节点).data);在printContents函数中 但是我不太确定它到底出了什么问题 以下是链接列表代码: void createEmptyLinkedList(LinkedList *inList) { inList = (LinkedList*)malloc(sizeof(LinkedList));

我是一个初学者,正在学习如何用C语言制作链表。每当我试图打印链表时,链表打印出来的效果都很好,但最后总是出现分段错误

当我使用GDB backtrace时,它指向行->条目=*((*节点).data);在printContents函数中

但是我不太确定它到底出了什么问题

以下是链接列表代码:

void createEmptyLinkedList(LinkedList *inList) {
    inList = (LinkedList*)malloc(sizeof(LinkedList));

    (*inList).head = NULL;
    (*inList).tail = NULL;

    (*inList).size = 0; //Keeps track of size of list

    return;
} 
void insertAtStart(LinkedList *inList, JournalEntry *inValue) {
    LinkedListNode *newNode;
    int listSize = (*inList).size;

    newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));

    (*newNode).data = inValue;
    (*newNode).next = (*inList).head;
    (*inList).head = newNode;
    ((*inList).size)++;
    return;
}

void printContents(LinkedList *inList) {
    LinkedListNode *node;
    JournalEntry entry;

    node = (*inList).head;

    while (node != NULL) {

            entry = *((*node).data);

            printf("%04d-%02d-%02d: %s\n", entry.year, entry.month, entry.day, entry.text);

            /*Move node to the next node*/
            node = (*node).next;
    }
    printf("Done!");
    return;
}
//Free nodes recursively
void freeLinkedList(LinkedList *inList) {
    freeNode((*inList).head);
    free(inList);
    return;
}

void freeNode(LinkedListNode *node) {
    if (node != NULL) {
    freeNode((*node).next);
    free(node);
}
以下是用于启动链接列表的主要功能:

int main() {
    LinkedList list;
    JournalEntry *value;
    char* textToEnter;

    value = (JournalEntry*)malloc(sizeof(JournalEntry));

    createEmptyLinkedList(&list);

    textToEnter = "Hello";
    (*value).day = 10;
    (*value).month = 5;
    (*value).year = 2010;
    strcpy((*value).text, textToEnter);
    insertAtStart(&list, value);

    printContents(&list);

    freeLinkedList(&list);
    return 0;
}
为了防止任何人需要它,下面是头文件中声明的结构:

typedef struct LinkedListNode {
    JournalEntry *data;
    struct LinkedListNode *next;
} LinkedListNode;
typedef struct {
    LinkedListNode *head;
    LinkedListNode *tail;
    int size;
} LinkedList;
typedef struct {
    int day;
    int month;
    int year;
    char text[1000];
} JournalEntry;

C中的所有内容都是通过值传递的,包括指针。因此,分配给inList不会影响调用者传递的值。相反,如果您想这样做,您应该将指针指向指针:

void createEmptyLinkedList(LinkedList **inList) {
    *inList = malloc(sizeof(LinkedList));

    (*inList)->head = NULL;
    (*inList)->tail = NULL;

    (*inList)->size = 0; //Keeps track of size of list

    return;
}
如果没有这个,您只需要使用一个未初始化的指针来保存列表。在主代码中,您还需要对其进行如下更改:

LinkedList *list;
createEmptyLinkedList(&list);

请注意,列表在这里声明为指针。

在我看来,问题是您没有决定是
createEmptyLinkedList()
为头部分配内存,还是在
main()
函数中分配内存。
你两个都做了。
main()

你做了
链接列表--此部分创建LinkedList结构。
然后将该结构的地址传递给函数。这一点很好。

createEmptyLinkedList()
中,有指向列表结构的inList指针。这也很好。

但是现在你把事情搞得一团糟,
malloc()
另一个LinkedList结构,使得
inList
指向新的malloc结构。

然后初始化全新的LinkedList结构,从
createEmptyLinkedList()
返回,而不更改
main()
中的
list
结构,因为您初始化了新的
malloc'd
结构,非list

您可以通过决定是在
main()
还是
createEmptyLinkedList()
中创建
LinkedList
结构来修复它 如果你选择第二个——阅读上面的答案。
但如果您选择第一,请保持一切不变,并删除负责malloc'ing的
createEmptyLinkedList()
中的行。


然后,正如FatalError所建议的那样-更适合您函数的名称应该是initializeEmptyLinkedList,因为不再有创建部分。

与您的问题没有特别的关系,只是为了将来的参考,任何形式的
(*foo).bar
可以重写为
foo->bar
。您是否检查了
数据
是否具有有效值?或者只是删除malloc()部分,因为这会把事情搞砸。然后按原样进入初始化部分。也许除了丑陋的
(*inList)。数据部分。@zubergu:当然。这也是一个有效的解决方案。尽管在这一点上,我认为这个函数有点命名错误(create vs init)。你是绝对正确的。