Kyle Hsieh (謝阿Sa)
    • 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
    # Note for [你所不知道的C語言:指標篇](https://hackmd.io/hqJBzualRcOrb2wMhheKCQ?view) ```cpp void **(*d) (int &, char **(*)(char *, char **)); ``` `void C(*d)(A, B)` - d is a **function pointer** that takes *A and B parameters* - Function pointer: 指向Function的指標 - Parameters of a function are separated by comma `,` `A: int &` - parameter A is a **reference** to an int - Reference: 該變數在memory的位置 `B: F(*)(D, E)` - 與d一樣是一個function pointer - `D: char *` a pointer to a char - `E: char **` a ++pointer++ to a ++pointer++ to a char - `F: char **` return a type like *E* /History/ [C語言是用來開發Unix](https://zh.wikipedia.org/wiki/B%E8%AA%9E%E8%A8%80) - from Year 1972-1974 - 能夠充分掌握硬體 - C語言代表的文化就是Unix - 可以自己編譯自己 ## 先羅列你已經知道的部份 - C語言:超好懂的指標 - Operator `&`: address/reference of - Operator `*`: value/de-reference of ## 回頭看C語言規格書 - C99 \[6.2.5\] _**Types**_ > The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’. Derivative 的 KK 音標是 \[dəˋrɪvətɪv\],而 derivation 的 KK 音標是 \[d,ɛrəv’eʃən\] - An array type of unknown size is an incomplete type. - A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. > 關鍵描述!規範 `void *` 和 `char *` 彼此可互換的表示法 ``` void *memcpy(void *dest, const void *src, size_t n); ``` ## 英文很重要 - `declare a as array of pointer`: ```c *a[] ``` ## `void *` 之謎 - 對某硬體架構,像是 ARM,我們需要額外的 alignment。 沒有「雙指標」只有「指標的指標」 ---------------- - 指標的指標是有**從屬**關係的 - C 語言中,萬物皆是數值 (everything is a value),函式呼叫當然只有 call-by-value。 Pointers vs. Arrays ------------------- ``` int main() { int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; printf("%d %d %d %d\n", x[4], *(x + 4), *(4 + x), 4[x]); } ``` - \*(x + 4): 4, 是4個整數大小的意思 - C language沒有真正的陣列/矩陣 Function Pointer ---------------- - [x] to-be studied ### C99 [6.3.2.1] Lvalues, arrays, and function designators #### Lvalues - [你所不知道的C語言:指標篇#Lvalue的作用](/@sysprog/c-pointer?view#Lvalue-的作用) - 延伸閱讀: [Understanding lvalues and rvalues in C and C++](http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c) - *lvalue* in C/C++ is defined to **locator** value. An Basic example: ![](https://i.imgur.com/i7XFW50.png) - **Modifiable lvalues** C99 [6.3.2.1] 1: A modifiable lvalue is an lvalue that **does not** have array type, does not have an incomplete type, does not have a **const-qualified** type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type. Example: ![](https://i.imgur.com/0Y88pYy.png) - **Conversions between lvalues and rvalues** ![](https://i.imgur.com/4RziLlE.png) - the unary address-of operator '&' takes an lvalue argument and produces an rvalue, here's an example and an issue found when tracing by GDB: The issue: 指標(從)的指標(主)的值與指標(從)的值對應的記憶體位置不一致。 ```cpp int var = 10; int *bad_addr = &(var + 1); // ERROR: lvalue required as unary '&' operand int *addr = &var; // OK: var is an lvalue &var = 40; // ERROR: lvalue required as left operand // of assignment ``` 用GDB trace的結果如下: ![](https://i.imgur.com/G0TUBdT.png) 在執行第3步利用address-of operator時,發現pointer的address就儲存在該pointer指向的位置的下一個word (紅箭頭處);於是下一步就以該pointer指向的位置為起始,印出4個words,可以看到pointer的指向位址是'd(-8100);下一步我們該decimal value cast成pointer value (a constant value type which points to void type), **得到MSB不一致的值**。 做了一些猜想,是virtual address(VA)與physical address(PA)的轉換?利用*objdump*查看,VA跟PA是一致的;因為剛好是MSB差'1', 所以想到可能跟binary expression中signed bit有關,就改用hex將該4個words印出來,看到下個word的值剛好就是預期的位址的high-order word (黃框處): ![](https://i.imgur.com/l6W2uCr.png) 因為我們使用的Linux系統是x86_64的版本,而const的value type是signed double word, 所以做了**Sign Extension**; 而正確的位址是由兩個words所組成。 - CV-qualified rvalues What is this "cv-unqualified" thing? _CV-qualifier_ is a term used to describe __const__ _and_ __volatile__ _type qualifiers_. ![](https://i.imgur.com/NGiKy04.png) Learn C The Hard Way -------------------- - Never use gets(), because we doesn't known the length of string from ++stdin++; change to fgets(). 重新探討「字串」 -------- Proper nouns --- ### designate | 美 ˈdɛzɪɡˌnеt | | 英 ˈdеziɡnеit | vt. (past designated, pp designated, present designating) 1. 標出;表明;指定 ▸ Churches are designated on the map by crosses. 地圖上教堂以十字形記號標出。 2. 把……定名為,稱呼 \[(+as)\] \[O9\] ▸ The ruler of a kingdom is designated a king. 王國的統治者稱作國王。 3. 委任,指派 \[(+as)\] \[O2\] \[O9\] ▸ He was designated to lead the expedition. 他被指派率領遠征隊。 a. 指定的,選定的 \[A\] ▸ the minister designate部長指定人選 n. **designator** # Q & A 1. what's meaning of "**"? - It means "a pointer of pointer". C 語言中,萬物皆是數值 (everything is a value),函式呼叫當然只有 call-by-value。「指標的指標」(英文就是 a pointer of a pointer) 是個常見用來改變「傳入變數原始數值」的技巧。 - Ref.: https://hackmd.io/@sysprog/c-pointer?view#沒有「雙指標」只有「指標的指標」 ###### tags: `sysprog2021` `system_programming` `C LANGUAGE` `jserv`

    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