0

Why we dont get the address of character type array (char arr[] = "abc";when we print it like " cout << arr<< endl; but if we print a integer array we get the address of the a[0]?

#include <iostream>

using namespace std;

int main()
{
    int a[] ={1,2,3};
    char b[] = "abc";
    cout << a << endl;
    cout << b <<  endl;
}

in output i got the memmory address of a but for b i got abc please explain why

  • The C++ standard library provides `std::ostream& operator<<(std::ostream, char const*);` A char array decays into a pointer. That is not provided for `int const*`, so `void const*` is the best match for an `int` array (after pointer decay), which prints out the pointer value. – Eljay Nov 07 '22 at 14:00
  • 1
    From the olden days of C, `char` arrays are used to hold strings, so most of the standard library code treats `char*` as a pointer to the first element of a nul-terminated sequence of characters. – Pete Becker Nov 07 '22 at 17:08

0 Answers0