POV-Ray : Newsgroups : povray.binaries.programming : [C++] Question about reinterpret_cast<> : [C++] Question about reinterpret_cast<> Server Time
20 Apr 2024 07:14:09 EDT (-0400)
  [C++] Question about reinterpret_cast<>  
From: Tim Chan
Date: 21 Nov 2001 13:40:20
Message: <3bfbf514$1@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.