Eutherpy
Eutherpy

Reputation: 4571

Access violation error in doubly linked list

I got this error while trying to add an element to my doubly linked list. I can't seem to find the mistake.

struct Pacijent
{
    char ime[10];
    [...]
    Pacijent *prev;
    Pacijent *next;
};

struct Lista
{
    Pacijent *front;
    Pacijent *back;
};

void assign(Pacijent p1, Pacijent p2)
{
    memcpy(&p1.ime, &p2.ime, sizeof(p1.ime));
    [...]
}

Here's the function that causes the mistake:

void insertBack(Pacijent p, Lista l)
{
    Pacijent *novi = (Pacijent*)malloc(sizeof(Pacijent));
    assign(*novi, p);
    if (l.back = NULL)
    {
        l.front = l.back = novi;
        novi->prev = NULL;
        novi->next = NULL;
    }
    else
    {
        novi->prev = l.back; //here is where I get the error
        l.back->next = novi;
        novi->next = NULL;
        l.back = novi;
    }
}

and the corresponding part of main function:

Lista *lista = (Lista*)malloc(sizeof(Lista));
    lista->back = lista->front = NULL;

    [...]
    Pacijent p1 = noviPacijent("Marko", "Markovic", "Milan", jmb1, 1.75, 70, 23);
    [...]

    insertBack(p1, *lista);

The insertBack function seems correct to me, I can't really locate the problem. Thank you in advance.

Upvotes: 0

Views: 64

Answers (1)

Mahesh Bansod
Mahesh Bansod

Reputation: 2033

3rd line of the instertBack function: if (l.back = NULL)
You may want to change it to : if (l.back == NULL)

Upvotes: 1

Related Questions