What does this code entail?

Hey guys,

This book I'm following gives questions but no answers which is strange but this is one of them.

"Will the three memory addresses displayed by the program be the same? Explain what's going on in the program."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>

using namespace std;

int main() {
	int a = 10;
	int& b = a;
	int* c = &b;

	cout << &a << endl;
	cout << &b << endl;
	cout << &(*c) << endl;

	return 0;

}


Now I know the memory addresses will be the same. So first an integer variable named "a" is declared then initialized to 10.

"b" is declared as a reference type and refers to the object value held by "a".

Lastly an integer pointer is declared named "c" which points to the memory address of the object "b" refers to which is the memory address of the object value stored in "a".

Last edited on
closed account (iAk3T05o)
The name of the book is beginning c++ with game programming, 3rd edition.
Could be. Why wouldn't it explain detailed answers to the questions? :(

Is the above correct though? :)
closed account (iAk3T05o)
I don't know. Unlike in jumping into c++ where the answers are at the end pages, it doesn't have answers.
But i switched from jumping into to c++ to beginning c++ through game programming.
also you don't need to output the pointers address like that.


 
cout << c << endl;
will do the trick.
Topic archived. No new replies allowed.