# 【3-5】指標 我們每個人的家都有地址,當然,變數也有。 ## 指標 我們要怎麼取得變數的地址(記憶體位置),或者怎麼靠地址(記憶體位置)找到變數,就得依賴指標(Pointer)。 ### 宣告 ```cpp int a = 10; int* p = &a; // 指標 p 儲存變數 a 的地址 ``` ### 取值與取址 ```cpp cout << "a 的值: " << a << endl; // 顯示 a 的值 cout << "a 的地址: " << &a << endl; // 顯示 a 的地址 cout << "p 指向的地址: " << p << endl; // 顯示指標 p 儲存的地址 cout << "p 指向的值: " << *p << endl; // 解引用指標,顯示 p 指向的值(即 a 的值) ``` ### 陣列 ```cpp int arr[3] = {1, 2, 3}; int* p = arr; // 陣列名稱 arr 是陣列首元素的位址,也就是 &arr[0] cout << *p << endl; // 1 cout << *(p + 1) << endl; // 2 ``` ### Python對照 Python 中沒有指標這個概念,變數是物件的參考,不可以直接取得記憶體位址或操作指標。 --- 聯絡方式:codecodefunny@gmail.com 最後編修時間:2025/07/10 子柚筆
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up