Return the kind of an expression in C, i.e., whether it's an rvalue or lvalue

J. Doe

Can I print how an expression is being evaluated?

For example if I wanted to find out whether a value was being evaluated as an rvalue or lvalue I'd call the hypothetical code:

int main() {
    if(isrvalue(*(int *)4)) return 0;
    else return 1;
}

This creates problems as we're discovering below that the 'type' of an expression can depend on whether it's on the right or left side of the assignment operator. So the test would be more suited as

supports_lvalue(*(int *)4)

Although again this is only hypothetical and may be left to just playing around with basic examples.

The reason is only for experimentation but it might be useful in debugging if it's possible.

J. Doe

According to the C Standard, section 6.3.2.1:

An lvalue is an expression (with an object type other than void) that potentially designates an object.

Although this is rather vague, it continues with

The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue.

And further more

What is sometimes called ‘‘rvalue’’ is in this International Standard described as the ‘‘value of an expression’’.

What this means to me is

An lvalue is a value that supports assignment and is on the left side of the assignment operator.

An rvalue is any value not on the left hand side of the assignment operator.

So for example

#include <stdio.h>

int main() {
    int array[1], *pointer, i = 42;
    //                      ^    ^-rvalue
    //                    lvalue

    *array = i;
    // ^     ^-rvalue
    //lvalue

    pointer = array;
    // ^         ^-rvalue
    //lvalue

    printf("%p\t%d\n", pointer, *pointer);
    //                    ^-rvalues-^

    // causes segfault but compiles:
    *(int *)4 = 2;
    //      ^   ^-rvalue
    //   lvalue

    return 0;
}

As above in the comments and to answer the question fully

You'd have to use a compiler that lets you access the AST of a given compilation unit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ does reference return a lvalue or rvalue?

From Dev

Is the expression that allocates memory rvalue expression or lvalue expression?

From Dev

Does the expression `new T` evaluate to an rvalue or an lvalue?

From Dev

Is an expression involving function type an lvalue or an rvalue?

From Dev

Why cast expression to rvalue reference to function is lvalue?

From Dev

return statement binding rvalue reference to an lvalue?

From Dev

Rvalue or Lvalue?

From Dev

Is assignment operator in c++ returns rvalue or lvalue?

From Dev

C++ Operator Overloading [ ] for lvalue and rvalue

From Dev

C binary operator applyed on width of LVALUE or RVALUE

From Dev

C++ function signature that accepts lvalue, rvalue and rvalue ref

From Dev

C, Expression must be a modifiable lvalue (changing the value of a struct's member)

From Dev

Regular expression to find lvalue and rvalue in source code (ignore == )

From Java

Why does operator* of rvalue unique_ptr return an lvalue?

From Dev

Why does this function return an lvalue reference given rvalue arguments?

From Dev

Strange behavior in GCC: rvalue ref and lvalue ref are covariant return types

From Dev

C - expression must be a modifiable lvalue

From Dev

(c) expression must be a modifiable lvalue

From Dev

How to move from both rvalue and lvalue arguments in C++?

From Dev

C++, take const lvalue and rvalue reference in a function

From Dev

Why C++ lvalue objects can't be bound to rvalue references (&&)?

From Java

Rvalue reference or lvalue?

From Java

getting lvalue and rvalue of a declaration

From Dev

Rvalue Reference is Treated as an Lvalue?

From Dev

Passing an Lvalue to a parameter of RValue

From Dev

Lvalue to rvalue reference binding

From Dev

Initializing lvalue reference with rvalue

From Dev

lvalue and rvalue assignment error

From Dev

lvalue to rvalue implicit conversion

Related Related

  1. 1

    C++ does reference return a lvalue or rvalue?

  2. 2

    Is the expression that allocates memory rvalue expression or lvalue expression?

  3. 3

    Does the expression `new T` evaluate to an rvalue or an lvalue?

  4. 4

    Is an expression involving function type an lvalue or an rvalue?

  5. 5

    Why cast expression to rvalue reference to function is lvalue?

  6. 6

    return statement binding rvalue reference to an lvalue?

  7. 7

    Rvalue or Lvalue?

  8. 8

    Is assignment operator in c++ returns rvalue or lvalue?

  9. 9

    C++ Operator Overloading [ ] for lvalue and rvalue

  10. 10

    C binary operator applyed on width of LVALUE or RVALUE

  11. 11

    C++ function signature that accepts lvalue, rvalue and rvalue ref

  12. 12

    C, Expression must be a modifiable lvalue (changing the value of a struct's member)

  13. 13

    Regular expression to find lvalue and rvalue in source code (ignore == )

  14. 14

    Why does operator* of rvalue unique_ptr return an lvalue?

  15. 15

    Why does this function return an lvalue reference given rvalue arguments?

  16. 16

    Strange behavior in GCC: rvalue ref and lvalue ref are covariant return types

  17. 17

    C - expression must be a modifiable lvalue

  18. 18

    (c) expression must be a modifiable lvalue

  19. 19

    How to move from both rvalue and lvalue arguments in C++?

  20. 20

    C++, take const lvalue and rvalue reference in a function

  21. 21

    Why C++ lvalue objects can't be bound to rvalue references (&&)?

  22. 22

    Rvalue reference or lvalue?

  23. 23

    getting lvalue and rvalue of a declaration

  24. 24

    Rvalue Reference is Treated as an Lvalue?

  25. 25

    Passing an Lvalue to a parameter of RValue

  26. 26

    Lvalue to rvalue reference binding

  27. 27

    Initializing lvalue reference with rvalue

  28. 28

    lvalue and rvalue assignment error

  29. 29

    lvalue to rvalue implicit conversion

HotTag

Archive