Archived

This topic is now archived and is closed to further replies.

memory addressess

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

quote:
Original post by ShadowHunter
that code only prints out 5, I need the memory address, as in where the variable is located in the memory.
& is the "address of" operator. Saying &a means the "address of a".

This *should* do it:
int a = 5;
std::cout << &a << std::endl;

Share this post


Link to post
Share on other sites

int a = 5;
int *memaddress = &a;

printf("%d\n",memaddress); // prints out memory address


printf("%d\n",*memaddress); // prints out 5



See the difference?

Share this post


Link to post
Share on other sites
quote:
Original post by ShadowHunter
is there anyway to edit something in that memory address, without knowing the variable name?
Yes, but of course you need to get the address initially from someplace (ie. the variable name):
int main()
{
int var = 10;
int *var_ptr = &var; // holds the address of var

std::cout << var << std::endl; // Outputs ''10''

*var_ptr = 5; // Assigns value 5 to the int, pointed to by var_ptr

std::cout << var << std::endl; // Outputs ''5''

return 0;
}

Share this post


Link to post
Share on other sites