-1

While practicing with C++, I tried to display a char variable's address with the & operator, but I see a meaningless line of characters instead of a memory address.

Below is the complete program:

#include <iostream>
using namespace std;

int main()
{
    char a;
    a = 'a';

    cout << &a;
}

Below is the output display I see:

Memory address of a char variable in C++

I have tried to search online to find a solution and detailed information about this issue, but I couldn't find anything useful.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ekanlar
  • 1
  • 1
  • `&a` will return `char*` and `cout` thought you want to print a string that pointed by `&a`. So you could just cast it to `int` like this `cout << int(&a)` – Huy Nhat Tran Jul 16 '23 at 15:42
  • There's an [`operator <<` overload](https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2) for handling C strings. Just cast it to something else, like `static_cast(&a)` – Useless Jul 16 '23 at 15:44
  • Your program has undefined behavior due to using a non-null terminated character buffer to `cout`. – PaulMcKenzie Jul 16 '23 at 15:48
  • If you want to print a pointer, then print a pointer, don't cast it to an integer. `operator<<` has an overload for `void*`, which all pointers are convertible to, eg: `cout << static_cast(&a)` – Remy Lebeau Jul 16 '23 at 18:50
  • The output you see was caused by your IDE filling the memory with `0xCC `in debug builds as a debug aide. Remember to recognize this symbol: [https://stackoverflow.com/a/127404/487892](https://stackoverflow.com/a/127404/487892) – drescherjm Jul 17 '23 at 01:01

1 Answers1

3

A pointer to a character - char * - is treated as a pointer to a C-style string, and printed as if it were one, even though it isn't in your program: a C-style string is an array of characters and then a terminating NUL byte. Because that NUL byte is not there, meaningless garbage is printed.

C-style strings predate C++ and <iostream>, but they are still a part of the language; string literals like "hello" are also C-style strings, for example.

If you want to print the address, one way is to cast the pointer to void *:

#include <iostream>
using namespace std;


int main()
{
    char a;
    a = 'a';

    cout << static_cast<void *>(&a);
}

That way, you get the overload of operator<< that can print any kind of pointer as its memory address, not the overload of operator<< that prints C-style strings.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • 2
    Minor point: `char *` is treated as a **pointer to** a C-style string. A C-style string is a nul-terminated **array** of `char`. +1. – Pete Becker Jul 16 '23 at 15:46
  • "*Because that NUL byte is not there, meaningless garbage is printed*" - that is one possible outcome. Technically, the behavior of printing a null-terminated string without a null terminator is *undefined*, so literally anything could happen. You could print garbage. You could crash the app. You could trash memory. And so on. – Remy Lebeau Jul 16 '23 at 18:52
  • I am confused about the "pointer" part. What I understand is that I create a char variable, not a char pointer. And the "&" symbol is being called as "the address-of operator" in all sources I read. So, if I create an integer, int a, I get the memory address of the variable by displaying &a. Why doesn't it work with a char variable the same way? – ekanlar Jul 17 '23 at 11:09
  • &a is a pointer to a, which makes it's type `char *`. cout has a specific overload for `char *` – stark Jul 17 '23 at 13:22
  • Autocorrect inserted the wrong its. – stark Jul 18 '23 at 13:40