0

When I write the code

int main(){
    char c = 'a';
    char *p = &c;
    cout<<*p<<" "<<&c<<" "<<p<<" ";

    return 0;
}

The output is

a aa aa

Why this behaviour only in case of char when other datatype returns the address? And y an extra a appended with the char content?

sara
  • 71
  • 6
  • 1
    You are seeing [undefined behaviour](http://en.cppreference.com/w/cpp/language/ub) – Passer By Mar 06 '18 at 03:44
  • Along with https://stackoverflow.com/questions/18727022/access-array-beyond-the-limit-in-c-and-c explains what you are seeing – Passer By Mar 06 '18 at 03:45
  • 1
    If you want to print the addresses, use `cout<<*p<<" "<<(void*)&c<<" "<<(void*)p<<" ";` – Eljay Mar 06 '18 at 03:50

0 Answers0