# non-lvalue in assignment

#### Alex Xander

Joined Jun 23, 2013
1
Code:
Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct { char name[30];
char lastname[30];
int  points;
}PLAYER;
PLAYER input();
PLAYER input()
{
PLAYER A;
printf("Name?\n");
scanf("%s",A.name);
printf("Last Name?\n");
scanf("%s",A.lastname);
printf("Points?\n");
scanf("%d",&A.points);
return(A);
}
int main()
{
int i,n,*niz;
niz = (int*)malloc(n*sizeof(int));
do{
printf("Number of Players?\n");
scanf("%d",&n);
}while(n<1);

for(i=0;i<n;i++)
(niz+i)=input();
system("pause");
}
Line 30: non-lvalue in assignment
I just need an explanation, why did the error occurred, how do I fix it, and what should I do to prevent that in future. Thanks

Last edited by a moderator:

#### ErnieM

Joined Apr 24, 2011
8,171
Exactly what was your intent when you wrote

(niz+i)=input();

Was it that :

niz = input() - 1;

An "lvalue" is an object or variable that can be assigned the value of the right side. (niz+1) is an expression.

#### Papabravo

Joined Feb 24, 2006
16,509
niz is a pointer to an integer. bniz + i is also a pointer to an integer that is time sizeof(int) away from niz. What you want to do is "de-reference" the pointer with the '*' (star) operator.

Try:
*(niz+i) = input() ;
The way to read such an expression is "the integer pointed at by (niz+i)"
In compiler terms the star operator makes an l-value out of an address expression.

#### WBahn

Joined Mar 31, 2012
26,398
Line 30: non-lvalue in assignment
It would help if you gave some indication of which line is line 30, instead of making us count and hoping that you included exactly the code that the compiler was basing its line numbers on. Also, format your code so that it is easy for others to read. If you want a stranger to look over your code, then it is to your benefit to make your code easy to look over. Doesn't that seem reasonable?

I just need an explanation, why did the error occurred,

how do I fix it,
By correcting the mistake.

and what should I do to prevent that in future.
Don't make that mistake again.

To expect a more meaningful answer, it is generally helpful to give us an indication of what you were trying to get the code to do. As it is, we see what you told the code to do; obviously the two aren't the same, but you are only letting us see one side. So the best we can do is guess -- and why do you want to make the strangers you are asking for help have to guess at what you were trying to do?

Based on the error message, I'm guessing (notice -- guessing!) that the line that is being complained about is:

(niz+i)=input();

What would be wrong with the following:

42 = 3 + 17;

The expression on the left side of the assignment operator can't be just a number because what the assignment operator does is it evaluates the expression on the right side of the operator and gets a value. It then stores that value at a memory location indicated by the expression on the left side of the operator. So what does it mean to store the value 20 in the value 42? Nothing! It's is a meaningless statement -- and so when you tell the compiler to do something like this, it complains and tells you that the expression on the left side of the assignment operator is not an 'lvalue', meaning something that can be written to.

But what if you wanted to write the result of 3+17 not to the value 42, but rather to memory location whose address is 42? Well, that's a perfectly reasonable thing to do. We do this using the "indirection" or "dereference" operator, which is the '*' character. So

*42 = 3 + 17;

is perfectly valid (though it may throw a runtime exception if you don't actually have permission to write data to that location).

What this says is, "Make the value stored at the address 42 equal to the value obtained by evaluating the expression 3+17."

Which brings us to your code:

(niz+i)=input();

'niz' is a pointer to an integer. This means that 'niz' is a variable, just like any other variable, and that it stores a value of a particular type, namely a value this is normally used as a memory address. But that is all it is -- a value stored in a variable.

The exoression (niz+i) is just an expression that evaluates to a value. In this case, it evaluates to a number that happens to be the address of the ith integer after the address stored in the variable 'niz'. But that's all it is -- a value, just like 42.

To use it on the left side of an expression, you need to dereference it. So

*(niz+1) = ...

can be read as, "Make the value stored at the address obtained by evaluating the expression (niz+1) equal to ...."

See how that works?