#include<stdio.h>
int ways(int n,int m)
{
    if(n=0) return m;
    if(m=0) return n;
    else ways(n,m)=ways(n-1,m)+ways(n,m-1);   //there is an error here.pls explain//
    return ways(n,m);
}
int main()
{
    int a,b;
    printf("give a and b");
    scanf("%d %d",&a,&b);
    ways(a,b);
    printf("the no of ways from (%d,%d) to (0,0) are %d",a,b,ways(a,b));
    return 0;
} 

what is an lvalue. and whats the wrong in my code in line 6? very curious to know about it. thank you

Edited 3 Years Ago by Dani: Formatting fixed

Please use code-tags and format your code in a sensible way.

Your problem is that you can't do this:

ways( n, m ) = ways( n-1, m ) + ways( n, m-1 );

ways() is a function, returning an int, it makes no sense to try and assign a value to the int that it returns.

An lvalue is a value that can appear on the left of an assignment. If a function returns by value, then that value is an rvalue - it can only be on the right of an assignment.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

what is an lvalue. and whats the wrong in my code in line 6? very curious to know about it. thank you

#include<stdio.h>
int ways(int n,int m)
{
   if(n=0) return m;
   if(m=0) return n;
   else ways(n,m)=ways(n-1,m)+ways(n,m-1); //there is an error here.pls explain//
   return ways(n,m);
}
int main()
{
   int a,b;
   printf("give a and b");
   scanf("%d %d",&a,&b);
   ways(a,b);
   printf("the no of ways from (%d,%d) to (0,0) are %d",a,b,ways(a,b));
   return 0;
}

When posting code, use the code tags so your code appears as above. And I added a little indentation to your code to help ease the pain.

An lvalue is a left value, that is, left of the "=" sign (pronounced "assignment operator" sign - not equals sign). There are left values (lvalues) and right values (rvalues). Here is an example of an lvalue and a rvalue used in an expression:

int a = 0;
int b = 0;
int c = 0;

a = (b + c);  //a is an lvalue and (b + c) is an rvalue.

The variable "a" can hold a value, that is, it's a container that can hold a value - a memory location. That is what is required on the left side of an assignment operator. It only makes sense that "a" has to be a container (memory location) in order for it to be assigned the value of (b + c). Now (b + c) (an rvalue) cannot hold a value, it doesn't represent a memory location like a variable does. (b + c) is an expression, and is very temporary, almost as though it is being calculated in mid-air so the result can be placed into "a" and then it is discarded. All rvalues are like that - they just represent a transient thing without any solid substance, with no place to call home. Obviously then, they cannot be assigned a value, they do not represent a memory location in which to hold a value. If you did this:

(b + c) = a;

then you would be saying: "Place the value of "a" into (b + c) and thus the compiler would issue an error something like this: "lvaule required on left side of assignment operator".

An rvalue can only be on the right side of an assignment operator (except in special cases, like a pointer returned from a function). However, an lvalue can be used on the right side of "=" (as above) or on the left side.

And when you see this: a = 10; then you should get into the habit of saying, "a is assigned the value of ten" instead of saying " a equals ten. Then you wouldn't make the kind of mistake that you made in lines 4 and 5 of your program. There you stated that n is assigned 0 and so is m. If they are both assigned 0, then they will always be false and the if statement will always skip the statement(s) that follow.

Now, with your new knowledge of l and r values, see if you can determine why there is an error on line 6.

This article has been dead for over six months. Start a new discussion instead.