Quantcast
Channel: User Alex Lop. - Stack Overflow
Viewing all articles
Browse latest Browse all 39

Answer by Alex Lop. for Can someone explain to me why my factorial recursion code can't be compiled

$
0
0
factorial(i) = (factorial(i - 1) * i);

This line is invalid in terms of C standard. It is related to the lvalue and rvalue definition.

lvalue - is an expression referring to an object.The name 'lvalue' comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression.

rvalue - is an expression which is not lvalue (I couldn't find the exact definition of it). In other word, rvalue cannot be re-assigned.

Example:

int n;...n = 3; // --> Legal, n is an lvalue and 3 is an rvalue3 = n; // --> illegal, 3 is rvalue and thus n cannot be assigned to it

Another example

int a, b, c;...a = b + c; // --> Legal since 'a' is an lvalue (refers to an object/memory)/* Note that 'b' is also lvalue, 'c' is also lvalue BUT 'b + c' is an rvalue expression! *//* the temporal storage allocated for the result of the expression 'b + c' cannot be "visible" *//* one cannot check the address of such expression: &(a + b) ==> illegal */b + c = a; // --> Illegal

In you example, factorial(i) represents the return value of the function which is an rvalue.

For more details: https://www.embedded.com/lvalues-and-rvalues/


Viewing all articles
Browse latest Browse all 39

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>