Hello,

I've cast the address of a string to a char* . I expected to be able to use the pointer in the same way as other types, but it behaves quite differently. Could someone explain what is going on?

Why does it print the string and not the address? How does it know where the string ends? Ho *do* I print the address of a char in the string, like for e[2] (I would expect this to be the address of the 3rd letter, s)?

Here is the code. I've compared it to an int*.
Code:
#define DEBUG
#ifdef DEBUG                                    
#define P(A) cout <<  #A << ": " << (A) << endl;// prints variables
#define NL cout << endl;                        // new line
#else
#define P(A)                                    // empty definitions
#define NL
#endif

#include <iostream>
#include <string>
using namespace std;

void main() {

  int a[2];
  a[0] = 5; a[1] = 7;
  string s = "test text";
  
  int* ap = a;
  char* e = (char*)s.c_str();  // string elements

  NL; P(s); P(&s);

  NL; P(e); P(e[0]); P(&e); P(&e[0]); P(&e[2]); P(&(e[0]));

  NL; P(ap); P(ap[0]); P(&ap); P(&ap[0]); P(&ap[1]); P(&(ap[0]));
}
And here is the output

Code:
s: test text
&s: 0012FF7C

e: test text
e[0]: t
&e: 0012FF40
&e[0]: test text
&e[2]: st text
&(e[0]): test text

ap: 0012FF50
ap[0]: 5
&ap: 0012FF44
&ap[0]: 0012FF50
&ap[1]: 0012FF54
&(ap[0]): 0012FF50