Pavel Korobov
Pavel Korobov

Reputation: 78

C: "lvalue required as left operand of assignment" while using pointers in dynamic structures

The program must to scan sequence of numbers before zero, then scan one more number and delete all the numbers which are equal to the last.

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int elem;
    struct node *next;
};

void print(struct node *list)
{
        while (list != NULL)
        {
                if (list -> elem != 0)
                        printf("%d ", list -> elem);
                list = list -> next;
        }
        printf("\n");
}

void push_back(struct node *list, int value)
{
        struct node *ptr;
        ptr = (struct node*)malloc(sizeof(struct node));
        ptr -> elem = value;
        ptr -> next = NULL;
        while (list -> next != NULL)
                list = list -> next;
        list -> next = ptr;
}

int pop_front(struct node *list)
{
        int value;
        struct node *ptr;
        ptr = list -> next;
        value = ptr -> elem;
        list -> next = ptr -> next;
        free(ptr);
        return value;
}

int main()
{
        struct node head = {0, NULL};
        int x;
        scanf("%d", &x);
        while (x != 0)
        {
                push_back(&head, x);
                scanf("%d", &x);
        }
        print(&head);
        scanf("%d", &x);
        while (head.next != NULL)
        {
                if (head.elem == x)
                        pop_front(&head);
                &head = head.next;
        }
        printf("\n");
        print(&head);
        return 0;
}

The trouble is in the line

&head = head.next;

5.c:65:10: error: lvalue required as left operand of assignment

But what's wrong? Didn't I do the same in, for example, function print to move through the list?

Upvotes: 0

Views: 1045

Answers (5)

Emil Laine
Emil Laine

Reputation: 42838

&head is the memory address of head. You can't change the address of variables.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126243

In the print function, to move through the list, you do:

list = list -> next;

while in the main function, you do:

&head = head.next;

See the difference?

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311018

The result of applaying operator & is not an lvalue. It is a temporary value that is equal to the address of its operand.

Take into acccount that your design of the list is wrong. Either the head should be allocated dynamically as all other nodes in the list or you should define one more structure named like list that will contain pointer to the head node.

For example either declare head like this

struct node head = NULL;

Or define one more structure

struct list
{
    node *head;
};

//...

int main( void )
{
    struct list lst;
    //,,,

But in the both cases you should to rewrite other functions.

Upvotes: 0

Viet
Viet

Reputation: 583

You must use head as a pointer. If you declare it as static variable, you can not change the address of it. So the code &head=head.next is not valid

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

Quoting C11 standard, chapter §6.5.16

An assignment operator shall have a modifiable lvalue as its left operand

In your code, head is a variable of type struct node and the address of that variable is not a modifiable lvalue.

To assign a value to some variable, you need to have a modifiable lvalue, and &head is not a modifiable lvalue.

You may want to have a pointer instead, to which you can assign another address (remeber, not to the address of the pointer).

Upvotes: 0

Related Questions