Reputation: 119
Given two variables:
int x
int* p
and these expressions:
*(&x+*p)+x
&p+x
*(&p-(int**)&x)+x
*(&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.
I think the first one is not valid.
Should be an Rvalue of type int, am I right?
What about the others? Thank you for your help!
Upvotes: 0
Views: 230
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.
*(&x+*p)+x
&p+x
*(&p-(int**)&x)+x
*(&x+*(p+7))
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?
Should be an Rvalue of type int, am I right ?
No, you are not. It is a pointer + integer
, which results to a pointer.
*(&p-(int**)&x)+x
This is essentially *(pointer-pointer) + integer
. As pointer-pointer
is integer and thus cannot be dereferenced, ...
*(&x+*(p+7))
This is *(pointer + *(pointer+integer))
, so *(pointer + integer)
. Can that be assigned to?
Upvotes: 4