pointerS, HOW COME my answer is different from the book

how come my answer is different from the book? are the addresses not the same?

Integer age = 30
Integer dogsAge = 9
pointsToInt points to age
pointsToInt = 0x00CFFCA4
*pointsToInt = 30
pointsToInt points to dogsAge now
pointsToInt = 0x00CFFC98
*pointsToInt = 9


book has it at

Integer age = 30
Integer dogsAge = 9
pointsToInt points to age
pointsToInt = 0x0025F788
*pointsToInt = 30
pointsToInt points to dogsAge now
pointsToInt = 0x0025F77C
*pointsToInt = 9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main()
{
	int age = 30;
	int dogsAge = 9;

	cout << "Integer age = " << age << endl;
	cout << "Integer dogsAge = " << dogsAge << endl;

	int* pointsToInt = &age;
	cout << "pointsToInt points to age" << endl;

	// Displaying the value of pointer
	cout << "pointsToInt = 0x" << hex << pointsToInt << endl;

	// Displaying the value at the pointed location
	cout << "*pointsToInt = " << dec << *pointsToInt << endl;

	pointsToInt = &dogsAge;
	cout << "pointsToInt points to dogsAge now" << endl;
	cout << "pointsToInt = 0x" << hex << pointsToInt << endl;

	cout << "*pointsToInt = " << dec << *pointsToInt << endl;

	return 0;

}
Last edited on
Your pointer addresses will usually vary with each run. Nothing is wrong.
thank you!
at little more on that,
a pointer is the location in your computer's ram where the operating system chose to store the data for your program. The choice of this location is predictable (if we know the algorithm, like open source linux) but too complicated for a human to figure out without a great deal of hands on effort and diagnostics into the box. This is not true, but lets pretend the algorithm to pick memory is that it just linearly starts at location 0 and finds the first unused block large enough to store your data in it. Depending on what is running at that instant in time, the next available slot is always in motion as other programs ask for and give back the memory resource. So the location you get back is, as already said, is not consistent across runs etc.
Topic archived. No new replies allowed.