C {}ローカル変数の内部と外部、アドレスと値の使用を宣言する
コードと実行結果
最初にコードを見てください:
#include <iostream>
using namespace std;
int main()
{
int i = 5;
int j = 6;
cout << "outer i address is : "<< &i << endl;
cout << "outer j address is : "<< &j << endl;
cout << "out1 i+j = " <<i+j << endl;
for(int i = 0;i<5;i++)
{
cout << "inner i address & value is : " << &i << "------"<<i << endl;
cout <<"inner1 i+j = " <<i+j << endl;
for(int j = 4;j<5;j++)
{
cout << "inner j address & value is : " << &j << "------"<<j << endl;
cout << "inner2 i+j = " <<i+j << endl;
}
}
cout << "out2 i+j = " <<i+j << endl;
cout << "******************************************************************" << endl;
return 0;
}
上記は主に、forループの内外で宣言を繰り返した後のijのアドレスと値の変更と使用を確認するためのものです。
結果を見てください
結論として
1.変数名は同じですが、アドレスが異なります
。2。内部変数が最初に使用され、割り当ては外部に影響を与えません。