Hello, I'm studying C++ on my own and would appreciate some help.
In the following Program:-
#include <iostream>
using namespace std;
const int sz = 100;
struct X { int a[sz]; };
void print(X* x) {
for(int i = 0; i < sz; i++)
cout << x->a[i] << ' ';
cout << endl << "--------------------" << endl;
}
int main() {
X x;
print(&x);
int* xp = reinterpret_cast<int*>(&x);
for(int* i = xp; i < xp + sz; i++)
*i = 0;
print(reinterpret_cast<X*>(xp));
} ///:~
I don't understand this line:
**** print(reinterpret_cast<X*>(xp)); ****
why do you pass 'xp' into the cast but not '&xp'....?
Because earlier you need to pass the address of x into the cast:
**** int* xp = reinterpret_cast<int*>(&x); ****
Please help me cos i've been thinking about this for so unnecessarily long.
Cheers, Tim.
Post a reply to this message
|