user2067720
user2067720

Reputation:

Segmentation faults in the code

I keep getting segmentation faults but i have no clue why, i figured out where my segmentation fault was but dont know how to fix it.

struct node {
        int line;
        int count;
        char* word;
        struct node* next;
};

struct node* nodeGetPreviousNode (struct node* head, struct node* node)
{
        //return the previous node given the node
        while(((head) != NULL) ||((head)->next != node))
        {
                (head) = (head)->next;
        }
        return (head);
}

Upvotes: 1

Views: 88

Answers (1)

simonc
simonc

Reputation: 42215

while(((head) != NULL) ||((head)->next != node))

will evaluate (head)->next != node, dereferencing head when head is NULL

Did you mean to use && instead?

Upvotes: 8

Related Questions