You are on page 1of 2

Memory Addresses 1

//Customize for your compiler if necessary

//Formatting may change with this web posting

#include <iostream>

using namespace std;

int main() {

int x; // x is a 'normal' int variable

int y; // y is a 'normal' int variable

int *ptr1, *ptr2; //multiple declarations on same line

int *mx; // to store memory address of variable x

int *my; // to store memory address of variable y

mx = &x; // get memory address of variable x

my = &y; // get memory address of variable y

*mx = 10; // put 10 in the location referred to by memory address mx

*my = *mx + 3; // add 3 to the value in the location referred to by memory

// address mx, then put result in the location referred

// to by memory address my.

cout << "*mx is " << *mx << endl;

// value stored at the location referred to by memory

// address mx should be 10

cout << "x is " << x << endl;

// x should be 10, too, since it refers to the same value

// as *mx
cout << "y is " << y << endl;

// y should be 13, since it refers to same value as *my

system("pause");

/*

*mx is 10

x is 10

y is 13

*/

Last modified: Wednesday, 24 January 2018, 12:06 PM

You might also like