Tony041010
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: 2021CRC title: 指標 & 參照 slideOptions: transition: slide theme: --- # 指標 & 參照 ## 2021/11/05 電算社第七堂社課 --- ## 記憶體 ---- 是電腦儲存變數值的地方, 可以把它想像成是<font color="#FFF300">一個很長的紙帶</font> 上面有很多<font color="#FFF300">連續的格子</font>,每一個格子有<font color="#FFF300">連續的編號</font> 每個格子是 1 bit(0 or 1) 8 個 bit 組成 1 byte ![](https://i.imgur.com/sgeOA6h.png =80%x) --- ## 指標 Pointer ---- 指標是一種變數, 裡面存放一個變數的第一個記憶體位置(address) 可以視為它是<font color="#FFF300">"指向該變數的變數"</font> ---- 宣告 ```cpp= int *ptr; ``` 指向(賦值) ```cpp= int a = 100; int *ptr = &a; //宣告一個指標ptr,指向 a 的記憶體位置 //&是取址的意思,也就是取得a的記憶體位址 ``` ```cpp= //或者是 int *ptr; int b = 100; ptr = &b; //宣告一個指標ptr,指向 b 的記憶體位置 ``` ---- 取值 ```cpp= int a = 100; int *ptr = &a; cout << ptr << " 、 " << *ptr << endl; //*ptr可以知道ptr指向的變數的值 //輸出結果 : a的記憶體位置 、 100 ``` ---- 改值 ```cpp= int a = 100; int *ptr = &a; cout << ptr << " 、 " << *ptr << endl; //輸出結果 : a的記憶體位置 、 100 *ptr = 110; //它會直接更動指向的變數(a)的值 cout << ptr << " 、 " << *ptr << endl; //輸出結果 : a的記憶體位置 、 110 ``` ---- ### 小差異(常用錯!!) ```cpp= int a = 10, b = 20; int *ptr = &a; //ptr這個指標指向a ptr = &b; //現在改成指向b *ptr = a; //這個跟上一行不一樣,ptr仍舊指向b,但是b的值被改為與a相同 ``` --- ## reference(參照) ---- reference可以理解成是一個變數的<font color="#FFF300">別名</font> 它不像指標會儲存記憶體位置 他反而可以<font color="#FFF300">直接操控該變數</font> ---- ```cpp= int a = 10; int &b = a; //宣告一個reference b,指向a,想像它是a的綽號 ``` ---- 應用 ```cpp= int a = 10; int &b = a; cout << a << ' ' << b << endl; //輸出 10 10 b = 100; //會間接讓a也變成100 cout << a << ' ' << b << endl; //輸出 100 100 ``` --- ## 指標 vs 參照 ---- 直接看例子吧:D ---- ```cpp= #include <iostream> using namespace std; int main(){ //宣告 int a = 2; int *pt = &a; // pt是一個指標變數,存取a的記憶體位置 int &r = a; // r是一個參照,為a的綽號 cout << a; // 2 cout << &a; // a的記憶體位置 cout << *&a; // 2 cout << pt; // a的記憶體位置 cout << &pt; // pt的記憶體位置 cout << *pt; // 2 cout << r; // 2 cout << &r; // a的記憶體位置 cout << *&r; // 2 return 0; } ``` --- ## 函式 ## call by value & call by address & call by reference --- ### call by value ---- call by value就是傳值的意思, <font color="#FFF300">呼叫變數時是複製一份數值,</font> 而非將記憶體連動過去 上一堂課中的那些引數都是 call by value ---- Example: ```cpp= #include <iostream> using namespace std; void add(int value_a){ value_a++; cout << value_a << '\n'; //2 } int main(){ int a = 1; add(a); cout << a; // 1 return 0; } //上面的函式將a的數值複製一份到value_a,所以改變value_a的值並不會改變a的值 ``` ---- 那如果想<font color="FFF300">改變值</font>的話該怎麼辦呢 這時候就必須<font color="#FFF300">回傳</font> ---- ```cpp= #include <iostream> using namespace std; int add(int value_a){ value_a++; return value_a; } int main(){ int a = 1; cout << add(a); // 2 return 0; } ``` --- ### call by address ---- call by address 就是傳指標,傳到函式時用指標連動到原先的變數,所以改變指標時原先的值也會改變 ---- Example: ```cpp= #include<iostream> using namespace std; void add(int *pointer_a){ (*pointer_a)++; //記得加(),因為 ++ 的優先度高於取值(*) } int main(){ int a = 1; add(&a); //記住要傳的是 a 的位置 cout << a; // 2 return 0; } //pointer_a 是個指向 a 記憶體位置的指標 //所以改變 pointer_a 的值 a 也會變 ``` --- ### call by reference ---- call by reference就是傳參照,呼叫變數時會直接連動記憶體,所以改變值時原變數的值也會跟著改變 ---- Example: ```cpp= #include <iostream> using namespace std; void add(int &reference_a){ reference_a++; } int main(){ int a = 1; add(a); cout << a; // 2 return 0; } //reference_a和a的記憶體位置是共用的 //所以改變reference_a的值也就改變了a的值 ``` --- ### 小練習 ---- 試著做出一個可以調換數值的函式吧,規定此函式回傳值必須為void,且輸出必須放在main函式中 ---- 輸入說明:輸入兩個數a, b 輸出說明:輸出a, b調換數值後的結果 範例輸入:1 2 範例輸出:2 1 ---- 我是防雷頁:D ---- 常見錯誤 ```cpp= #include <iostream> using namespace std; void swap(int a, int b){ int t = a; a = b; b = t; } int main(){ int a, b; cin >> a >> b; swap(a, b); cout << a << b; return 0; } //call by value的函式會複製一份數值進去函式,所以不會改變原來的值 ``` ---- 解答(call by address) ```cpp= #include <iostream> using namespace std; void swap(int *a, int *b){ int t = *a; *a = *b; *b = t; } int main(){ int a, b; cin >> a >> b; swap(&a, &b); cout << a << ' ' << b; return 0; } ``` ---- 解答(call by reference) ```cpp= #include <iostream> using namespace std; void swap(int &a, int &b){ int t = a; a = b; b = t; } int main(){ int a, b; cin >> a >> b; swap(a, b); cout << a << ' ' << b; return 0; } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    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

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully