Problem related to strings

if we have char arr[] then why does cout<<arr; print the string and not the address as it was in the case of integers?

Hello @vipin.kumar,

  1. In the documentation of C++, the cout<< has many overladed functional definitions. (You will learn about overloading in detail in Object-oriented module).

  2. When it accepts the address of char type, the compiler instead of printing the address the variable prints the characters until it encounters a ‘\0’ character.

  3. It works correctly for other types but operates differently for char type.

  4. To overcome this, you can typecast it to some other type preferably void.
    Example:
    cout<<(void *)arr;

Hope, this would help.
Give a like, if you are satisfied.

1 Like