Problem Displaying Address of Character
Feb 20, 2013 at 4:54pm UTC
When this programs runs it displays odd symbols for the address of the character. This is only part of the program, I took out the parts that already work. Is this supposed to happen? Any ideas on how to fix it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
using namespace std;
char again;
int main()
{
char character = '8' ;
char *ptr2;
ptr2 = &character;
do
{
cout << "Character" << endl;
cout << "Value: " << *ptr2 << endl;
cout << "Address: " << &character << endl;
cout << "Size: " << sizeof (character) << " bytes" << endl << endl;
cout << "Do you want to run this program again? Y/N: " ;
cin >> again;
} while (again == 'y' || again == 'Y' );
return 0;
}
Feb 20, 2013 at 4:57pm UTC
cout interprets a char* as a c-string. If it didn't, when you did: cout << "Character"
it would print the address of the string.
cout << "Address: " << static_cast <void *>(&character) << endl;
Feb 20, 2013 at 5:00pm UTC
Ah, I understand now. Thanks for the help!
Topic archived. No new replies allowed.