Following program prints the address of two different variables.
#include <iostream>
using namespace std;
void f1()
{
int i,k;
cout<<"f1 : "<<&i<<endl;
}
void f2()
{
int j;
cout<<"f2 : "<<&j<<endl;
}
int main() {
f1();
f2();
return 0;
}
When I run this program on GCC, I wondered, both variables have a same addresses.
Why both variable hold the addresses same?
Edit:
void f1()
{
int i = 10;
cout<<"f1 : "<<&i<<endl;
cout<<"value of : "<<i<<endl;
}
void f2()
{
int j;
cout<<"f2 : "<<&j<<endl;
cout<<"value of j : "<<j<<endl;
}
int main() {
f1();
f2();
return 0;
}
In this example, if I assigned value 10 to i then j also print 10. I think it is not valid because j is uninitialized.