1

I want to receive a variable of type DWORD which will contain address pointer float *.
If I write this:

float *Object = 5;  
std::cout << &Object;

It gives me exactly the value that I want (i.e: 0F235C1A). Can you tell me how to assign this value to DWORD for using it in my Memory Write function?
Trying:

DWORD ObjAddress = &Object;

I got compiler error: cannot convert from 'float *' to 'DWORD'

6
  • 2
    I doubt your first code does what you expect it to. Also what's the definition of DWORD? Commented Jun 25, 2015 at 11:30
  • What about typecasting? (DWORD) &Object Commented Jun 25, 2015 at 11:30
  • @Jefffrey DWORD is a microsoft typedef'd unsigned long Commented Jun 25, 2015 at 11:30
  • You may want to show your MemoryWrite function too. Commented Jun 25, 2015 at 11:30
  • 1
    float *Object = 5; shouldn't compile, for one thing. What compiler are you using? Commented Jun 25, 2015 at 11:32

3 Answers 3

2

I don't understand your object definition:

float *Object = 5;  

The 5 should be an address? a value?

See following code, maybe it is what you want.

float Object = 5;
int ObjAddress =  (int)&Object;
cout << "object adr1: "<< hex << &Object << endl;
cout << "object adr2: "<< hex << ObjAddress << endl;

Output

object adr1: 0x28ff08
object adr2: 28ff08

Or when you really need the object as a pointer

float value = 5;
float *Object = &value;
int ObjAddress =  (int)&Object;
cout << "object adr1: "<< hex << &Object << endl;
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to cast your &Object;

DWORD ObjAddress = (DWORD)&Object;

And I think that will solve your problem.

1 Comment

... and create another. Wielding C-casts blindly to shut the compiler up is not a good idea.
1

I think you can use reinterpret_cast like this (replace unsigned long with DWORD)

#include <iostream>

int main(void)
{
    float *object  = new float;

    *object = 5;

    unsigned long objAddress = reinterpret_cast<unsigned long>(&object);

    std::cout<<objAddress<<std::endl;
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.