跳至內容

out

考慮 waitpid 函式

lib C
  fun waitpid(pid : Int32, status_ptr : Int32*, options : Int32) : Int32
end

此函式的說明文件指出

The status information from the child process is stored in the object
that status_ptr points to, unless status_ptr is a null pointer.

我們可以這樣使用此函式

status_ptr = uninitialized Int32

C.waitpid(pid, pointerof(status_ptr), options)

這樣我們將 `status_ptr` 的指標傳遞給函式,讓它填入值。

使用 `out` 參數可以更簡單地撰寫以上程式碼

C.waitpid(pid, out status_ptr, options)

編譯器會自動宣告一個 `Int32` 類型的 `status_ptr` 變數,因為參數的類型為 `Int32*`。

這適用於任何 `fun` 參數,只要其類型是指標 (當然,前提是函式確實會填入指標指向的值)。