指標篇 心得
例子:
- d 是 pointer to a function 並且回傳 pointer to pointer to void
- 參數一: reference to int
- 參數二: pointer to function 並且回傳 pointer to pointer to char
- 參數一: pointer to char
- 參數二: pointer to pointer to char
C 語言只有 call-by-value
- ptr 是個 pointer type,對 ptr++ 來說,並不是單純 ptr = ptr + 1,而是遞增或遞移 1 個「單位」
- ex: int* ptr -> ptr++ 的單位就是 4 byte
輸出:
觀察 printf 和 i++ 、 ++i
printf
是由右往左開始執行的
++i
會先將 i
加一後回傳變數 i
i++
則是會先回傳 i
這個變數的值
輸出:
(練習題) 設定絕對地址為 0x67a9 的 32-bit 整數變數的值為 0xaa6,該如何寫?
- 絕對位址 –> (int* const) (0x67a9)
- assign 值進去: * (int* const) (0x67a9) = 0xaa6
void * 和 char * 彼此可互換的表示法
函式呼叫只有 call-by-value & 指標的指標
以上程式碼無法成功改到 *ptr 的值,因為函式是 call-by-value
所以運用 指標的指標 改成
Pointers vs. Arrays
- array 與 pointer 可互換
- x[i] 總是被編譯器改寫為 *(x + i) ← in expression
- 所以 x[4] *(x + 4), *(4 + x), 4[x] 可以達到相同的效果
- &b+1 -> 位移一個siezof(b)
- &b[0]+1 -> 位移一個sizeof(b[0])
- concatenate string s and string t
- 需要配置足夠的記憶體
- 記得釋放
- malloc可能執行錯誤
- array 會是用兩步取值,而 pointer 是三步。(array 的位址本身加上 offset,共兩步,而使用 pointer時,cpu 需先載入 pointer 位址,再用 pointer 的值當作位址並加上 offset 取值)
Function Pointer
- 函式只有再搭配 &, sizeof 時才不會被轉成 pointer to function
- 例子
- puts -> function designator
- *puts 的 puts 會被轉成 &puts (pointer to function returning type)
- 最後變成 *(&puts) , 最後結果等同於 function designator (puts)
- 所以上述例子等同
Address and indirection operators
- &(a[5]) 等同於 a + 5
- char str[123]
- str 等同於 &str
- str 因為不是遇到 sizeof 或 &,所以會被解讀成 pointer of type(這邊是 char)
- &str 就是 pointer to an array,剛好就是指向 str 的起始位址
指標的修飾 (qualifier)
- 指標所指向的內容不可變更 (Pointer to constant): const 在*之前
- const int*ptr1
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- int* const ptr2
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Lvalue & Rvalue
- lvalue是程式運行時可以通過地址(address)存取的值(比如通過 & 來取得)(或者說占用某記憶體空間),意味著他們是變數或者可以被 dereferenced (通常用 *)的某一塊特定記憶體位置
- rvalu
- ++ x 是左值,x ++ 是右值
- 所以 ++ (a ++) 會出錯
- 因為 a ++ 會回傳一個 rvalue(暫存值)
- 又 ++ x 的 x 必須是個 lvalue,因為 ++ x 需要先寫回data, 需要有個位址(adderss)可寫入
- 可以把 lvalue 當作 rvalue 使用,反之則不行
typedef
參考 c 語言 typedef 用法