Possible Duplicate:
Why is address of char data not displayed?
I was experimenting with ampersand operator and got stuck at this program :
#include<iostream>
using namespace std;
int main() {
char i='a';
cout<<&i;
return 1;
}
I was expecting the address of variable i as the output but instead the output came as the value of variable i itself.
Can anybody explain what just happened? Thanx in advance.
That’s because
cout::operator<<has an overload forconst char*. You’ll need an explicit cast to print the address:This will call the overload with
void*as parameter, which is the one used to print addresses.Also note that your code runs into undefined behavior. You only have a single char there, and the overload expects a null-terminated C-string.