0

Here in this code, cout<<q<<endl; is returning string "mani"?. q contain the address of first character 'm' so it should print address not string.please explain.

int main(){
    char *q;
    char b[5]={'m','a','n','i'};
    q=&b[0];
    cout<<b<<endl;
    cout<<q<<endl;
Stargateur
  • 24,473
  • 8
  • 65
  • 91

1 Answers1

3

std::cout has a special overload for const char*, which outputs the memory as an array of chars starting at the pointer passed up to the next NUL terminator (it's your job to make sure that the appropriate memory is available for that).

If you want to switch off this behaviour and output the pointer address then use a cast:

std::cout << (const void*)b << endl;
Bathsheba
  • 231,907
  • 34
  • 361
  • 483