0

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!

Zong
  • 6,160
  • 5
  • 32
  • 46
Atlas80b
  • 119
  • 1
  • 2
  • 8
  • Do you have a specific question about 3) or 4) ? What is your problem? What do you need to solve that? – Jens Gustedt Jun 06 '14 at 14:22
  • 1
    *I think the first one is not valid* Strictly speaking, it depends on the value of `*p`. If `*p > 1`, it's not valid, because you'd be computing a pointer to an element after one past the end of the 1-element array that `&x` represents. But yes, aside from the nitpick, it's an rvalue of type `int`. If you have any specific question about the others, then ask, but don't expect us to think for you - you have to ask a specific question. – Filipe Gonçalves Jun 06 '14 at 14:26
  • @FilipeGonçalves `*p > 0` is enough to make it invalid... – glglgl Jun 06 '14 at 14:28
  • @glglgl Hum, isn't it valid with `*p == 1`? After all, it's as if `x` is a one element array, and thus `&x+1` points to one element past the end - looks ok to me. – Filipe Gonçalves Jun 06 '14 at 14:30
  • @FilipeGonçalves It points to one element past the end, which is valid for a pointer expression - but you may not dereference it then. IOW, `&x+1` is valid, but `*(&x+1)` isn't. – glglgl Jun 06 '14 at 14:32
  • @glglgl Of course... nevermind, you're right. I was just thinking about the reference itself, heh – Filipe Gonçalves Jun 06 '14 at 14:33
  • @FilipeGonçalves `s/reference/pointer/` :-P – glglgl Jun 06 '14 at 14:36

1 Answers1

4

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?

Zong
  • 6,160
  • 5
  • 32
  • 46
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • the first one is valid, your adding an integer to an address(valid) dereferencing, and then adding an integer to the result(i am the -1 man, but i wont be when its fixed) – Steve Cox Jun 06 '14 at 14:29
  • @SteveCox I explain it in a slightly different way, but come to the same conclusion. – glglgl Jun 06 '14 at 14:31
  • @SteveCox Yes, the grey ones which are not in fixed-with type are supposed to be quotes. – glglgl Jun 06 '14 at 14:31
  • @glglgl thank you so much ! I still had some problems with indirection and adress-of operators. But thanks to your examples now I understood how to resolve this kind of exercises. – Atlas80b Jun 06 '14 at 14:37
  • So the last one is valid and it's an Lvalue of type int :) – Atlas80b Jun 06 '14 at 14:38
  • @Toxicroak Yes. But, as already mentionned, it will compile, but produce UB, as most offsets and indexes are out of bounds. – glglgl Jun 06 '14 at 14:39