Atlas80b
Atlas80b

Reputation: 119

Rvalue or Lvalue?

Given two variables:

int x
int* p

and these expressions:

  1. *(&x+*p)+x

  2. &p+x

  3. *(&p-(int**)&x)+x

  4. *(&x+*(p+7))

For each expression find out if it is valid or not, if it an Rvalue expression or an Lvalue expression and what type it represents.

  1. I think the first one is not valid.

  2. Should be an Rvalue of type int, am I right?

What about the others? Thank you for your help!

Upvotes: 0

Views: 230

Answers (1)

glglgl
glglgl

Reputation: 91017

I think the best way to answer is to explain what happens and put some "suggesting questions", and leave the decisions to you.

  1. *(&x+*p)+x
  2. &p+x
  3. *(&p-(int**)&x)+x
  4. *(&x+*(p+7))
  1. I think the first one is not valid.

    Why do you think that? It is all about pointer arithmetic.

    But you are in so far right as the access might be undefined. But that doesn't change anything on the qualification as Lvalue or Rvalue.

    Be aware that *(a+b) is the same as a[b], so it can be written as (&x)[*p] + x, i. e. int + int. Is it possible to assign anything to it?

  2. Should be an Rvalue of type int, am I right ?

    No, you are not. It is a pointer + integer, which results to a pointer.

  3. *(&p-(int**)&x)+x

    This is essentially *(pointer-pointer) + integer. As pointer-pointer is integer and thus cannot be dereferenced, ...

  4. *(&x+*(p+7))

    This is *(pointer + *(pointer+integer)), so *(pointer + integer). Can that be assigned to?

Upvotes: 4

Related Questions