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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 2021CRC Extra Resources title: 社課額外補充教材:指標 & 參照 --- # 社課額外補充教材 : 指標 & 參照 如果你覺得社課還算輕鬆,這裡提供你一些額外的補充教材。 這些是我們在備課的時候覺得稍微進階,或是篇幅太多,而沒有放在正式社課的內容。 --- 這邊有目錄,左邊也有,可以跳到自己想看的地方 [TOC] --- 建議必看的地方 : <font size=6>**全部**</font> --- ## 基本概念 指標的基本概念要從變數的資料到底存了什麼開始 當我們呼叫變數後,電腦會從記憶體隨機抓一個空間 而這空間會放着資料,也就是變數的值 --- 而指標不一樣,在宣告空間後,空間放的值不是變數的值 而是一個記憶體位置 ### 宣告方式 ```cpp= int *a; int* a; char *b; char* b; //當然也可以用其他資料形態宣告 ``` --- ### 專用的運算子 - \*(又叫做取值運算子) ```cpp= *a //a是記憶體位置 //這會回傳a位置的值 ``` - &(又叫做取址運算子) ```cpp= &b //b是一個資料,通常是變數 //這會回傳他的記憶體位置 ``` 爲什麼要先說這個而不是用法? 因爲就連最一般的賦值都要用到這個。 --- ### 用法 #### 賦值 我們在一開始的時候說過,指標存的是記憶體空間 那如果直接賦值的話呢? ```cpp= int* a = 10; ``` ![](https://i.imgur.com/CHe97f3.png) 出錯啦! --- c++為了避免有人亂打,將指標的值和一般的值做了區隔 也就是「int\*」,當然,如果是 char 的指標那就是 char* **那到底怎麼用?** 指標存的是記憶體位置,所以我們要指定一個位置給他 --- #### 賦值方法 ```cpp= #include<iostream> int main(){ int a = 10; //先創一個變數 int* pa = &a; //再指定為變數的記憶體位置 int b = 5; int c = 3; pa = &b; //也可以這樣,pa將會指向b *pa = c; //這就不一樣了 //這是讓pa指向記憶體的值(b的值)變成c的值 std::cout << a << ' ' << b << ' ' << ' ' << c << *pa; //將會輸出 10 3 3 3 } ``` --- 發現了嗎?因為指標的值是記憶體位置, 所以可以透過指標更改那個位置的值 上面賦值的解說 1. 因為指標不能存值,所以要透過變數 ```cpp= int a = 10; ``` 2. 之後再將指標指向變數 ```cpp= int *p = &a; //或是這樣 int *p; p = &a; ``` 3. 如果不是要重新指向,而是改變裡面的值的話用這個 ```cpp= *p = 15; ``` 請不要出現下面的寫法 ```cpp= int *p; p = &10; ``` 會出現錯誤,如果沒有錯誤,請還是不要用 如果沒有出現錯誤,那就是你的電腦沒有阻止你存取存放常數的地方 你如果更改了指標的值就可能會永久性更改那常數的值 --- ### 指標的另一種使用方式 我們知道指標必須仰賴另一個變數才能存值 但其實並非如此,它需要的是一個記憶體位置 也就是說,如果我們能為指標再開一個記憶體位置,我們就不需要變數 #### 先備觀念 記憶體有所謂的堆疊區(stack)和堆積區(heap) --- 堆疊區是我們一般宣告方式中會使用的 它在程式結束時會自動刪除,不管裡面還有沒有東西 不過,它的空間比堆積區還要小很多 而且變數以非常整齊的方式放置,並不完全是隨機存取 --- 堆積區會從電腦的記憶體中擴充空間,而非堆疊區是固定的大小 所以它能比堆疊區放更多的資料 不過相對的,它也不會自動刪除,需要透過人工刪除的方式 也因為這樣,堆積區的空間經常中間有未使用的地方 使得它不像堆疊區那樣整齊 --- #### 堆積區的使用 - 調用空間 ```cpp= int *n = new int; //後面是一個資料形態 //如果要用 char 那就是 new char int arr[64] = new int[64]; int arrarr[2][3] = new int [2][3]; ``` 注意:只能使用指標連接堆積區空間 - 刪除變數 ```cpp= delete n; //如果是陣列的話用下面這個 delete[] arr; delete[][] arrarr; ``` 透過這種方式,就能讓指標擁有能使自己存值的記憶體位置 ---- ## 函數中的使用方式 在這裡簡單說一下函式的概念 在數學有所謂的函數,程式也有 有回傳值的叫做函數,沒有回傳值的叫做函式 數學的函數: ```cpp= y = f(x) = //後面放對 x 的動作 //例如 y = f(x) = (x+1)*(x+1) ``` 當丟一個 $x$ 進去後,$f(x)$會經過它對 $x$ 的動作並回傳一個 $y$ 程式的函數: ```cpp= 回傳值的資料形態 函數名稱(參數1的資料形態 參數1在函數中的名字,參數2的資料形態 參數2在函數中的名字){ //裡面要放幾個參數都可但記得用逗號隔開 //這裡放動作 return 回傳值; //如果沒有回傳值,也就是回傳的資料形態是void //return 後面就不能放任何東西 } ``` 如果以上面的例子來寫 ```cpp= #include<iostream> int f(int x){ x = (x+1)*(x+1); return x; } int main(){ int x; std::cin >> x; int y; y = f(x); std::cout << y; } ``` 不過不要誤會了,參數在 main 函數的名字不用和他在 $f(x)$ 中的名字相同 也就是說我可以把上面的 $f(x)$ 改成這樣: ```cpp= int f(int input){ input = (input+1)*(input+1); return input; } ``` 結果會是一樣的 --- ### 有注意到嗎 不論我在 $f(x)$ 中對 $x$ 如何瞎搞,原本的 $x$ 都不會變 ```cpp= #include<iostream> int f(int x){ x = (x+1)*(x+1); return x; } int main(){ int x; std::cin >> x; int y; y = f(x); std::cout << x; } ``` 這樣只會輸出你原本輸入的值 這是因為參數在傳遞時有分三種方式,這一種會複製資料過去 --- ### call by value 使用方式: ```cpp= int f(int x){ x++; return x; } int main(){ x = 1; f(x); } ``` call by value 會複製參數的值到函式中 所以在main函式中的 $x$ 還是 1 --- ### call by address 使用方式: ```cpp= int f(int *x){ *x++; return *x; } int main(){ x = 1; f(&x); } ``` call by address 的概念是把變數的記憶體位置複製一份到函式中的指標 使函式中的指標指向原本的變數,並通過該指標更改原本變數的值 所以,main 函式中的 $x$ 會變成 $2$ --- ### call by reference 在說使用方式之前,先說說什麼是參照(reference) 參照並沒有記憶體空間,它只是一個變數的別稱 並非像指標一樣還會有個獨立的記憶體空間 但也因此,參照必須在宣告時指定為某一變數 而且無法更改 使用╱賦值方式: ```cpp= int a = 10; int &b = a; //這樣 b 就是 a 的參照了 b++; //a 也會變成 11 ``` 讓我們回到 call by reference 使用方式: ```cpp= int f(int &x){ x++; return x; } int main(){ int x = 1; f(x); } ``` 這樣,在 $f(x)$ 中的 $x$ 會被宣告為 main 函式中 $x$ 的參照 你可能會覺得很奇怪,為什麼名稱可以一樣? 這是因為在 $f(x)$ 中的 x 在 main() 中不存在,反之亦然 main 函式中的 $x$ 也會變成 2 --- 指標的概念博大精深,在之後教手刻 STL 的時候也會用到 必定要非常熟悉 <font size = 0.5>而且這樣你就贏過很多學長姐了</font>

    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