1

If:

int var = 0;
&var;   //--> returns the address of "var"

and:

int *p = &var;
*p;   //--> returns the value pointed to by "p", the value stored in "var"

then shouldn't (however redundant):

*&var;  //--> returns the value stored in "var"
*var;   //--> throw an error (or return the value stored in "var")?

I have no way to compile C++ code just now, but I'd like to figure out these operators.

  • 2
    There are free online compilers, e.g. ideone or Coliru – M.M Jul 10 '14 at 05:26
  • Just for reference, you can use an online compiler like [this one](http://ideone.com/), for example. – Theolodis Jul 10 '14 at 05:27
  • 1
    Probably a little bit too sophisticated for now, but you can come back to this one later. You can *overload* the address-of operator `&`. If this is the case, `*&var` may be different from `var`. I hope that you never face code where this is the case though. Related: http://stackoverflow.com/questions/6495977/what-legitimate-reasons-exist-to-overload-the-unary-operator – Markus Mayr Jul 10 '14 at 05:30

1 Answers1

4
*&var;  //--> returns the value stored in "var"

Yes, &var is a pointer that points to var, then *&var is the same as var.

*var;   //--> throw an error (or return the value stored in "var")?

This doesn't compile because you can't deference an int.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Can you only dereference address-values -- is there such a thing? If there is, how does the C++ interpreter 'know' a value is of type 'address'? If there isn't, how would the C++ interpreter not confuse a pointer with some other binary signal? Possibly one that I write? – DaveyDaGeek Jul 10 '14 at 05:33
  • 1
    The compiler "knows" because you declare variables to be a pointer type, or not. When you declared `int* p` you are saying to the compliler that `p` is of the type "pointer to an int". And the compiler will only allow pointer types to be de-referenced. – Ozraptor Jul 10 '14 at 05:36
  • Since this is C++, you should definitely write about references here. `*&var` yields a _reference_ to var, not its value, i.e `*&var = 17` will modify var. – Quentin Jul 10 '14 at 07:39
  • @Quentin `*&var` is an lvalue. But I'm not sure about the word *reference* here, I mean, there's no references in C, but `*&var = 17;` will modify `var` as well. – Yu Hao Jul 10 '14 at 07:48
  • They were not called "references" in C yet and weren't declarable/nameable indeed, but they were already there. In any case the question is tagged "C++", not "C" :) – Quentin Jul 10 '14 at 07:57
  • IIRC C also named them lvalue's. – MSalters Jul 10 '14 at 08:12
  • @MSalters I'm no language-lawyer so I may be mistaken on C terminology here. But I'm quite sure that in C++ "lvalue" refers to `var` itself, and something like `*&var` returns a lvalue reference (which is thus not an lvalue itself). – Quentin Jul 10 '14 at 08:24
  • @Quentin: It's not a function call, so it doesn't return anything. It's an "lvalue expression". As is `var` by itself. – MSalters Jul 10 '14 at 08:38