12

Possible Duplicate:
Why is address of char data not displayed?

Here is the code and the output:

int main(int argc, char** argv) {

    bool a;
    bool b;

    cout<<"Address of a:"<<&a<<endl;
    cout<<"Address of b:"<<&b<<endl;

    int c;
    int d;

    cout<<"Address of c:"<<&c<<endl;
    cout<<"Address of d:"<<&d<<endl;

    char e;    
    cout<<"Address of e:"<<&e<<endl;

    return 0;
}

The output:

Address of a:0x28ac67

Address of b:0x28ac66

Address of c:0x28ac60

Address of d:0x28ac5c

Address of e:

My question is: Where is the memory address of the char? And why is it not printed?

Thank you.

Community
  • 1
  • 1
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

3 Answers3

16

Strings in C/C++ can be represented by char*, the same type as &e. So the compiler thinks you're trying to print a string. If you want to print the address, you could cast to void*.

std::cout << static_cast<void *>(&e) << std::endl;
Charles Welton
  • 841
  • 7
  • 14
11

I suspect that the overloaded-to-char * version of ostream::operator<< expects a NUL-terminated C string - and you're passing it only the address of one character, so what you have here is undefined behavior. You should cast the address to a void * to make it print what you expect:

cout<<"Address of e:"<< static_cast<void *>(&e) <<endl;
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • What does ( void * ) do, can you please give a source ( a link or a search keyword ) for studying? – Koray Tugay Nov 23 '12 at 20:26
  • @KorayTugay it's a typecast, please use Google. (Just curious: you know what `static_cast` does, but not what `(void *)` means?) –  Nov 23 '12 at 20:26
  • No, I did not know that either. Yours just looked a bit easier so I decided to ask you. – Koray Tugay Nov 23 '12 at 20:29
  • So you are casting the address of e to a pointer? But why void? I will do some search and hopefully understand. – Koray Tugay Nov 23 '12 at 20:30
  • @KorayTugay Oh, I understand :) I have the exact opposite logic. –  Nov 23 '12 at 20:30
  • @KorayTugay because `void *` is a general pointer type. –  Nov 23 '12 at 20:31
  • Thanks. So it is like saying "Pointer of type ANYTHING" ? Thanks. Clear. – Koray Tugay Nov 23 '12 at 20:33
  • @KorayTugay Pretty much. Rather in C than in C++ (because C++ is more strictly typed, but in C, `void *` can point to any *data* pointer type - not to functions, though.) –  Nov 23 '12 at 20:34
  • @Yakk can you please explain why I should prefer `static_cast` over C-style casts? –  Nov 23 '12 at 20:38
  • static_cast shows up in text searches, which mattered a lot more pre-[clang](http://clang.llvm.org/), also and more importantly C casts are nearly unrestricted conversions and thus very error-prone. – jthill Nov 23 '12 at 20:47
  • @jthill Thanks. Didn't know that they were considered dangerous. (Well, I did... I always suggest not casting the return value of `malloc()`...) –  Nov 23 '12 at 20:49
  • 1) C style casts are hard to find in code. 2) C-style casts can easily ramp up from a simple operation (like converting a double to an int) to a dangerous one (like converting a `foo*` to an int). 3) Almost everything a C-style cast can do can be done with the appropriate C++-style cast. 4) If you really are reinterpreting memory (or whatever operation you want to do with a cast), using `foo* blah;reinterpret_cast(blah)` expresses what you are intending to do better than `foo* blah;(int)blah`. – Yakk - Adam Nevraumont Nov 23 '12 at 20:50
2

Check out this previously asked question: Why is address of char data not displayed?

Also, if you utilize printf("Address of e: %p \n", &e); that will work as well.

Community
  • 1
  • 1
Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41