0

This code:

string  str1 ( "Hello world" );
const char *c_str1 = str1.c_str ( );
cout << "The C-style string c_str1 is: " << c_str1 

generates this output:

The C-style string c_str1 is: Hello world

and I do not understand it.

c_str1 is a pointer, right? So, c_str1 should return an address and only *c_str1 should give the value located at this address. However, in the above example c_str1 gives the value (not the address).

What do I misunderstand?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    `&c_str1` would be the address of the pointer. You were thinking of `*c_str1`, except that that would give you the first `char` only. – Fred Foo Apr 09 '13 at 11:31
  • @larsmans, I thought that `&x` gives address of normal variable `x` and "address of pointer" is something strange since pointer is already an address. – Roman Apr 09 '13 at 11:33
  • 3
    Simplifying just a little bit, a pointer to `T` is a variable containing an address. That variable must live somewhere, so it too has an address, which is of type `T**` (pointer to pointer to `T`). – Fred Foo Apr 09 '13 at 11:35

6 Answers6

6

It's because of how std::cout::operator << is defined - it has an overload that takes a const char* and prints the string it points to. If you want the address, you'll have to cast to void*.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    I'd say "specified", not "implemented". It's implemented this way because that is how the standard specifies it. – James Kanze Apr 09 '13 at 11:31
4

"What do I misunderstand?" The definition of << on a char const*. The pointer is passed by value, but the definition of operator<<( std::ostream&, char const* ) specifies that it treat the pointer as the start of a '\0' terminated string.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

The variable c_str1 is a pointer to the first character in the string. &c_str1 is a pointer to c_str1 (i.e. a pointer to a pointer). *c_str1 is the value at the location pointed to by c_str1 (i.e. only a single character).

The output operator has an overload that takes a pointer to a character (what c_str1 is) and prints it as a string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

There is an overload of ostream& operator<< for const char*, which assumes the pointer points to the first character in a null terminated string, and prints the whole string.

You can see an example of this assumption being applied somewhere it shouldn't here:

#include <iostream>

int main()
{
  char c = 'x'; // not a null terminated string
  std::cout << &c << std::endl; // will write until it finds a 0 (really UB)
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

The standard specifies overloads of operator<< (ostream, char*) which output the string stored in the pointer. In other words, c_str1 is indeed a pointer, but the output stream is interpreting it as the string it points to.

To output the pointer value, cast it to void*:

cout << "The C-style string c_str1 is: " << static_cast<void*>(c_str1);
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

The cout operator<< interprets pointers to char as C-strings.

Leo
  • 1,174
  • 11
  • 25