Hello @Divya_321,
When you try to print the address of char variable using ‘&’(address of operator), it keeps on printing the characters present in the memory after the current location until it encounters a NULL or ‘\0’ character.
As we can see variable c has assigned a memory location: 0x7ffec265473f
So, when you do cout<<p<<endl; or cout<<&c<<endl;
The compiler is first printing the character present at 0x7ffec265473f i.e. ‘a’
then, it checks for next memory location 0x7ffec2654740 which contains ‘?’ and print it
Similarly, it prints the characters present at 0x7ffec2654741, 0x7ffec2654742, 0x7ffec2654743 and 0x7ffec2654744 i.e. ‘G’, ‘e’, ‘�’ and ‘�’ resp.
Now, it reaches the location 0x7ffec2654745 and it contains ‘\0’ character.
So, the further execution terminates.
To avoid this.
You are required to typecast this operator as you have done in the last two cout statements.
Note:
IT is possible that you may get different characters attached after ‘a’ for different executions of code or for executing it on different compilers.
You can also get ‘a’ as an output for the case when the next location contains ‘\0’ character.
Hope, this would help.
Give a like, if you are satisfied.