I have some errors to my code and I still don't understand why it does not work.

I have the following code snippet:

void insertBefore(List *lista, Node **node, Node *newNode)
{
    newNode->prev = (*node)->prev;
    newNode->next = (*node);
    if((*node)->prev == 0)
        lista->first = newNode;
    else
        (*node)->prev->next = newNode;
    (*node)->prev = newNode;
}

And I call it as:
insertBefore(lista,lista->first, newNode);

And the error is :

error: dereferencing pointer to incomplete type

What I tried and works(no errors but crashes when I debug):

void insertBefore(List *lista, Node **node, Node *newNode)
    {
        Node *anotherNode = (*node)->prev;
        newNode->prev = (*node)->prev;
        newNode->next = (*node);
        if((*node)->prev == 0)
            lista->first = newNode;
        else
            anotherNode->next = newNode;
        (*node)->prev = newNode;
    }

Here are the structures I use:

typedef struct NodeT
{
    struct nodeT *prev;
    struct nodeT *next;
    int key;
}Node;
typedef struct ListT
{
    Node *first;
    Node *last;
    Node *current;
}List;

Now, my question is: is there any issue when the compiler parses? I really don't figure it out.

有帮助吗?

解决方案

Watch the capitalization!

typedef struct NodeT      /* uppercase N */
{
    struct nodeT *prev;   /* lowercase N */

NodeT and nodeT are different identifiers

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top