Will the three memory addresses displayed by the following program all be the sa
ID: 3684632 • Letter: W
Question
Will the three memory addresses displayed by the following program all be the same? Explain what's going on in the code.#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; } Will the three memory addresses displayed by the following program all be the same? Explain what's going on in the code.
#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; }
#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; }
Explanation / Answer
#include<iostream>
using namespace std;
int main(){
int a = 10; // declaring a variable 'a' and initializing with value 10
int &b = a; // declaring a refrence variable of a, means b is alias of a, both denotes the same
int *c = &b; // declaring a int pointer 'c', initializing with address of b
// means *a(dereference), b, c are same
// so they are pointing to same value, stored at location, hence following program will print same memory location
cout<<&a<<endl;
cout<<&b<<endl;
cout<<&(*c)<<endl;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.