owned this note
owned this note
Published
Linked with GitHub
# 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: 
- **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:

- **Conversions between lvalues and rvalues**

- 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的結果如下:

在執行第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 (黃框處):

因為我們使用的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_.

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`