rvalue / lvalue

Jan 7, 2021 at 9:40pm
I am learning about r and lvalues and I think i am following the basic concept, but on the site I am learning from there is a piece of code that I am a bit confused with.

Where I am confused is where is says, p + 1 is an rvalue, but *(p + 1) is an lvalue, why is this? Is it because I can not assign to p + 1. Because p + 1 technically has a place in memory, or is it because when you do p + 1 that isnt saved anywhere in memory, but then neither is *(p + 1)

1
2
3
int arr[] = {1, 2};
int* p = &arr[0];
*(p + 1) = 10;   // OK: p + 1 is an rvalue, but *(p + 1) is an lvalue 
Jan 7, 2021 at 10:00pm
The key, from what I understand, is that you can't take the address of p + 1. p + 1 is not referring to a variable; it's just a temporary.
1
2
3
4
5
6
int main()
{
    int arr[] = {1, 2};
    int * p = &arr[0];
    &(p + 1);
}

is not valid.
 error: lvalue required as unary '&' operand
Last edited on Jan 7, 2021 at 10:04pm
Topic archived. No new replies allowed.