Search code examples
cpointershandlepointer-to-pointer

why handle to an object frequently appears as pointer-to-pointer


What is the intention to set handle to an object as pointer-to pointer but not pointer? Like following code:

FT_Library library;
FT_Error error = FT_Init_FreeType( &library );

where

typedef struct FT_LibraryRec_  *FT_Library

so &library is a FT_LIBraryRec_ handle of type FT_LIBraryRec_**


Solution

  • The 'C' library function FT_Init_FreeType has two outputs, the error code and/or the library handle (which is a pointer).

    In C++ we'd more naturally either:

    • return an object which encapsulated the success or failure of the call and the library handle, or

    • return one output - the library handle, and throw an exception on failure.

    C APIs are generally not implemented this way.

    It is not unusual for a C Library function to return a success code, and to be passed the addresses of in/out variables to be conditionally mutated, as per the case above.