-3

I am trying to output a simple char address on the display, but using & is not working:

#include <iostream>
using namespace std;
typedef int no;
typedef char nose;
typedef void laps;
laps noname(nose &boogers);
no main(no args, nose**LOC[])
{
cout << "Try ";
nose boogers = 't';
noname(boogers);
}
laps noname(nose &boogers)
{
cout << &boogers;
}

I have tried it by removing the ampersand of the parameter of laps, but the &boogers datum is not working either way because on the console it just shows the "t" character instead of an address.

Am I doing something wrong here?

JSYK: It compiles fine, no warnings at all. I just want to know why I am not getting an address instead of a value.

  • 2
    why redefine these POD types? It makes the code unreadable. – Matt Nov 15 '13 at 21:11
  • 3
    You're sending effectively `char*` to std::cout. there is an operator override for that (the string dump, if you want to call it that). If you want the address, `static_cast(&boogers)` – WhozCraig Nov 15 '13 at 21:14
  • 2
    `#define eat (void*)` and then `cout << eat &boogers;`. – jrok Nov 15 '13 at 21:15
  • @Matt I like to be unique. – Last Life Hump Nov 15 '13 at 21:17
  • @jrok I would do that, but #define is obsolete. – Last Life Hump Nov 15 '13 at 21:18
  • @LastLifeHump well unique or not, its *not* a good practice. Just imagine co-workers being equally "unique". Your combined code would look like utter chaos. – WhozCraig Nov 15 '13 at 21:18
  • @WhozCraig My code is more readable than your comment. What co-workers? I write this code individually. I would not adopt a habit of writing code like this on a team. – Last Life Hump Nov 15 '13 at 21:18
  • @LastLifeHump yeah, alright then. If you want to flame-troll there are better sites than this one. – WhozCraig Nov 15 '13 at 21:19
  • @WhozCraig Why use `const void*` for? I don't get what you wrote. I am not trolling, just genuinely wondering what's the issue with a character memory address. Why can't it be displayed normally? – Last Life Hump Nov 15 '13 at 21:20
  • I have never understood operator overriding/overloading. And I tried the code provided and it still is an error. – Last Life Hump Nov 15 '13 at 21:22
  • Read my comment again. The one that has more upticks than any other in this list. It is correct. There exists an override `operator <<` for sending a `char const *` to a `std::ostream&`. That override treats said variable as a nil-char terminated string, and attempts to send it as-such to the target stream. Sending the `&var` where `var` is of type `char`, `char&`, etc.. will invoke that override. If you want to send it as a raw address it can't be one that already has a formatted operator. See [the operators of `std::ostream`](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2). – WhozCraig Nov 15 '13 at 21:49

1 Answers1

0

After a reference is defined, it is not possible to refer to the reference itself, only the object it references. This is just how c++ operates. I suspect this is because references are not meant to be changed, this is one of their aspects which distinguishes them from pointers. Changing the memory address is prevented by not allowing you to access it.

bgardner
  • 45
  • 5