memory addressess

Started by
6 comments, last by ShadowHunter 20 years, 5 months ago
Hi, is there any way to figure out the memory address of your variables in C++?
Advertisement
type a & before your variable

int a = 5;

int *memaddress = &a

My Site
that code only prints out 5, I need the memory address, as in where the variable is located in the memory.
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;
int a = 5;int *memaddress = &aprintf("%d\n",memaddress);  // prints out memory addressprintf("%d\n",*memaddress);  // prints out 5


See the difference?

...keeping it simple...
*feels stupid*

Yeah I do, thanks
is there anyway to edit something in that memory address, without knowing the variable name?
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;} 

This topic is closed to new replies.

Advertisement