ひとりも見捨てないことを、あきらめない

学校教育、社会教育、数学、技術家庭科、Youtube、EdTech、ICT、プログラミング、その他

データの「ポインタ」について。備忘録。11/10 日

変数そのものと、変数のポインタ(アドレス)の関係について、「基本中の基本」ではありますが、備忘録としてメモしておきます。

 

サンプルのプログラム

#include <bits/stdc++.h>
using namespace std;

int main() {
 int x = 10; // x は変数である
 int *ptr = &x; // ptr は x を格納している場所(アドレス)を示している
 cout << "x: " << x << endl; // 10 になる
 cout << "ptr: " << ptr << endl; // アドレスその1
 cout << "&x: " << &x << endl; // アドレスその1
 cout << "&ptr: " << &ptr << endl; // アドレスその2 (prtのアドレス)
 cout << "*ptr: " << *ptr << endl; // もとの x の値(10)
}

 

実行結果

x: 10
ptr: 0x7ffc64610be4
&x: 0x7ffc64610be4
&ptr: 0x7ffc64610be8
*ptr: 10

 

f:id:takase_hiroyuki:20191110063736j:plain

 

f:id:takase_hiroyuki:20191110063740p:plain