reVolutionary
reVolutionary

Reputation: 65

Why wouldn't this pointer operation work?

So I have an array of linked lists

node* x = (node*) malloc(sizeof(node) * 500);

Now I want to insert into this list

int value;
value = 50;
node* ins;
ins = (node*) malloc(sizeof(node));
ins->val = value;
ins->next = &x[20];
&x[20] = ins;

Error i'm getting; 1value required as left operand of assignment

Incase you want to see the node struct

typedef struct node {
    int val;
    struct node* next;
 } node;

Why wouldn't something like this work? I want to learn :D

Edit: The error is pointed to this line

&x[20] = ins;

Upvotes: 1

Views: 81

Answers (3)

templatetypedef
templatetypedef

Reputation: 373422

The issue here is that when you write &x[20], you are getting back the address of the 20th element of the array x. Once you've created the array x, you can't change where its elements appear in memory. You can change the contents of the array elements, but where those elements are isn't going to change. As a result, writing &x[20] = ins is illegal, since the most natural interpretation of this statement is "move the 20th element of x to the spot pointed at by ins."

On the other hand, the code x[20] = ins is better (but it still has an issue). That means "change the contents of the 20th element of the array x to hold the address stored in ins." Keep in mind that there's a difference between "the address stored in x[20]" (which is given by x[20]) and "the address of x[20]," which is given by (&x[20]).

The problem with that line of code is that x[20] has type node and ins has type node*. Did you mean to make x a node** (a pointer to an array of node*s? If you make that change, that line should compile.

Technically speaking, the issue here is that the address-of operator returns an rvalue, but you can only put lvalues on the left-hand side of an assignment statement. An rvalue is a value like 137 or "hello" that represents, in some sense, a "pure value" that isn't the name of some place you can put something. Writing something like 137 = x isn't legal because you can't store x "in" 137, the same way that writing &x[20] = ins isn't legal because you can't store ins "in" &x[20].

Upvotes: 3

pm100
pm100

Reputation: 50210

i am guessing that x is an array of pointers to nodes. SO do

node** x = (node**) malloc(sizeof(node*) * 500);

You then want to store the address of your new node in a free slot of x

x[20] = ins;

although I am not sure where the 20 comes from

Your original construction

&x[20] = ins;

simply doesnt make sense , you are trying to change the address of an array member, not allowed

Upvotes: 1

MK.
MK.

Reputation: 34597

It is not 1value, it is lvalue, L as in LOVE.

&x[20] = ins;

says, approximately, take the 20th element in the array x. Find its address, change it. You can never change a pointer, you can only change the value that pointer is pointing to. So lvalue (an expression appearing on the left of assignment) can only be something like *node, but not &node. You don't need to allocate ans again. You already have a bunch of node objects allocated. You can just do

x[20].val = value;

Probably that's not what you want. But then then you can just change your x to be an array of pointers to node objects:

node** x = (node**) malloc(sizeof(node*) * 500);

then do

   x[20] = ins;

Upvotes: 1

Related Questions