i can't see anything while doing typecasting

I want to see typecasting pointer. So i write this program but the output is:

size of integer is: 4
address: 0053FC20, value: 2025
size of integer is: 1
address: Θ, value: Θ

how can do that?

my code is here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>

using namespace std;

int main()
{
	int a = 2025;
	int* p;
	p = &a;
	cout << "size of integer is: " << sizeof(int) << endl;
	cout << "address: " << p << ", value: " << *p << endl;
	char* pp;
	pp = (char*)p;
	cout << "size of integer is: " << sizeof(char) << endl;
	cout << "address: " << pp << ", value: " << *pp << endl;
}
The << operator is overloaded for char*, so that it treats the pointer as if it were a C-style string. So what you're seeing is the data in the memory pointed to by pp, interpreted as if it were an array of characters.
i understand, thank you
You're welcome!
the address is what it is, regardless of how you type it, by the way.

pp = (char*)p; //pp has the same value as p. the difference is that when you index it, eg pp[1] vs p[1] you move a different number of bytes from the starting point (the value in p and pp is the starting point). A more interesting program then, would print the address of p[1] and pp[1] as integers (hex or not) .. they will not be the same.
Last edited on
Topic archived. No new replies allowed.