C++ assign memory address?

Hello,

I'm still learning c++ so bear with me.

If i know the address lets say 0x28ff38 and i want to read that address how can i?

Obviously i can do this...

int *pInt;
int number = 0;

pInt = &number; // Now contains 0x28ff38.
cout << *pInt <<endl; // outputs 0
system("pause");

but what i want is to do something like...

int number = 0;
int *pInt = 0x28ff38; // address of number

cout << pInt << endl;

I hope you understand what i mean?
FalcubarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shajithchandranCommented:
you want to read the content of the memory 0x28ff38?

if yes.. then  normal star operator.

int *pInt = 0x28ff38; // address of number

cout << *pInt << endl;        <==== * operator on pint

If you want to read more data from that pointer location, then use memcpy
char buff[100];
memcpy(buff,pint,sizeof(buff));
Buff will contain 100 bytes of data located in the memory starting from the address where pint is pointing to
0
käµfm³d 👽Commented:
If I recall correctly, the following lines

    int *pInt = 0x28ff38; // address of number
    cout << pInt << endl;

as you have it will print the address of pInt, not the value it holds. Because you declared pInt as a pointer, you still need to dereference it to use  the value it points to. This is what you did in the top example
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
If this is really what you want to do, the only difference from the above is that you use the same cout line as the first example you posted, ie.

cout << *pInt << endl;

The more interesting question is are you sure that you want to be reading arbitrary memory locations? You don't mention what platform you are coding for, and I know that in some cases (ie. embedded development) that this may be a fairly normal thing to do, but if this is windows/linux/other advanced OS then doing this can be fairly dangerous and will often lead to runtime problems, etc.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

FalcubarAuthor Commented:
Thats what i had tried, but im getting this...

    int number = 1234;
    int *pInt = 0x28ff3c; // address of number

    cout << *pInt << endl;

Error:

 C:\C++\Learning\main.cpp In function `int main()':
13 C:\C++\Learning\main.cpp invalid conversion from `int' to `int*'
 C:\C++\Learning\Makefile.win [Build Error]  [main.o] Error 1

it doesnt like int *pInt = 0x28ff3c; // address of number

0
Infinity08Commented:
You cannot just assign an integer value (0x28ff3c is an integer value) to a pointer. This might make sense on some platforms, but there's no guarantee of that. Furthermore, it isn't very safe, so make sure that this is really what you want/need to do.

If you really do need to do this, you'll need to add an explicit cast, to re-assure the compiler that this is really what you want to do :
int* pInt = reinterpret_cast<int*>(0x28ff3c);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FalcubarAuthor Commented:
Maybe there is anther way then?

If i know the address of something, how would i read it? Thats all im trying to do.

I just want to say Read This address: 0x28ff3c or what ever address i choose.
I dont want to do things that are bad practice and if there is no way of doing it then np. Im still learning.

Thanks
0
Infinity08Commented:
>> I just want to say Read This address: 0x28ff3c or what ever address i choose.

Why that specific address ? How do you know which address to use ?
0
FalcubarAuthor Commented:
to be honest its just a learning question, i know addresses are not always going to be the same, im simply wondering if its possible. One of my friends, or a guy i know does some memory reading in things like WoW, the address values used by WoW seem to stay the same for things like HP, MANA, Playername, playerbase etc...

So im just wondering if it works like that or am i confusing myself?
0
Infinity08Commented:
>> im simply wondering if its possible

In that case, it is, as has been shown above.


>> One of my friends, or a guy i know does some memory reading in things like WoW, the address values used by WoW seem to stay the same for things like HP, MANA, Playername, playerbase etc...

Right. You're talking about cheating with a game then ?
0
FalcubarAuthor Commented:
No, that was the only way i could think of explaining what i was trying to get at :)

Thanks all for help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.