Adding nodes to a global linked-list

Posted by Zack on Stack Overflow See other posts from Stack Overflow or by Zack
Published on 2012-10-11T21:27:22Z Indexed on 2012/10/11 21:37 UTC
Read the original article Hit count: 201

Filed under:
|
|

I am attempting to construct my first linked list, and having read a basic introduction, have done the following. Firstly, declare a linked list node as:

struct errorNode {
    uint8 error;
    struct errorNode* next;
};

Secondly, define the first node globally as:

struct errorNode errorList = {0, NULL};

This has been done to allow each of the libraries that make up my current project to insert errors into a common list. The function to do this is:

void errorListWrite(uint8 error) {
    struct errorNode* newNode = malloc(sizeof(struct errorNode));

    newNode->error = error;

    newNode->next = &errorList;
    errorList = *newNode;
}

Whilst this compiles without error, it does not function as expected. I thnk the problem is with the last two statements of the list write function, but I am unsure. A hint as to what I am doing wrong would be most appreciated.

© Stack Overflow or respective owner

Related posts about c

    Related posts about linked-list