Casting Pointers? 
Author Message
 Casting Pointers?

How do you cast "lpPoint +=20;" to BYTE??

In Borland C++ you can do "(BYTE*)lpPoint +=20;" but how do you do it in
Visual C++??? I have tried almost anything...



Sun, 01 Apr 2001 03:00:00 GMT  
 Casting Pointers?
If you just want to increment your lpPoint to an offset 20 bytes away this
works:

lpPoint = (CPoint*)((BYTE*)lpPoint + 20); // a bit of a hack

if you actually want to assign that address to a BYTE pointer then this
works:

BYTE* tmp = (BYTE*)lpPoint + 20;

Jeff Scott
Dundas Software

Quote:

>How do you cast "lpPoint +=20;" to BYTE??

>In Borland C++ you can do "(BYTE*)lpPoint +=20;" but how do you do it in
>Visual C++??? I have tried almost anything...



Sun, 01 Apr 2001 03:00:00 GMT  
 Casting Pointers?

Quote:

>How do you cast "lpPoint +=20;" to BYTE??

>In Borland C++ you can do "(BYTE*)lpPoint +=20;" but how do you do it in
>Visual C++??? I have tried almost anything...

Borland's letting you do something illegal. Assuming lpPoint is a POINT*,
here's a legal way to do it:

 lpPoint = reinterpret_cast<POINT*>(reinterpret_cast<BYTE*>(lpPoint)+20);

It's ugly, but so is what you're doing. :) You can also use:

 lpPoint = (POINT*) ((BYTE*) lpPoint+20);

or even:

 reinterpret_cast<BYTE*&>(lpPoint) += 20;

Note that the result of all this is unspecified under the C++ Standard, but
it's legal code and will have the desired effect under VC.

--
Doug Harrison



Sun, 01 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. cast pointer to struct to pointer to type of first member of struct

2. can I cast pointer to pointers

3. can I cast pointer to pointers?

4. casting (pointer to array)

5. casting pointers

6. Casting pointers

7. correct way to cast pointers with offsets?

8. Casting pointers to union types

9. Casting pointers to functions

10. Casting pointers

11. cast pointers

12. Casting pointer reference

 

 
Powered by phpBB® Forum Software