C pointer to pointer
Object **self 是 C 語言的 pointer to pointer,常用在想在 callee 操作 caller 的資料。
1 | struct Object { |

C 語言是 call by value, self 的 value 是 &o 的 value,也就是 o 的 address。在 init_object() 對 self dereference、用 *self 操作等同用 o 操作,所以 *self = malloc(sizeof(Object)) 等同讓 o 指向新 allocate 出來的 memory,而 (*self)->a 等同 o->a 。o 是個 Object 的 pointer, o->a 是它指向的 Object 的 field a,(*self)->a = 0 是在初始化 main() 中 o pointer 指向的 Object。
如果不使用 pointer to pointer,像下面這樣會發生什麼事?
1 | int init_object(Object *self) { |
這時候 self 的 value 是 o 的 value,也就是 NULL。在 init_object() 裡 self 是個 local variable,allocate 一塊 Object 大小的 memory 給它以及其他操作都不會如我們希望的影響到 main() 裡的 o 。