1

Below is small snippet I have written

int main()
{
    char *c=new char('c');
    int *a = new int(10);
  
    cout <<c<<endl;
    cout <<a<<endl;
    return 0;
}

The output produced is

c                                                                                                                                             
0x217ac40   

Could someone explain why character is printed for c instead of address like a?

Meraj Hussain
  • 329
  • 1
  • 6
  • 24
  • This code has **undefined behavior**. The reason is because `operator<<` has an override to handle `char const*`, which expects a `'\0'` terminated C-style string, and outputs it accordingly. – Eljay Apr 27 '21 at 11:47

0 Answers0