c/language/pointer

Pointer is a type of an object that refers to a function or an object of another type, possibly adding qualifiers. Pointer may also refer to nothing, which is indicated by the special null pointer value.

Syntax
In the of a pointer declaration, the  sequence designates the pointed-to type (which may be function or object type and may be incomplete), and the  has the form:

where may be the identifier that names the pointer being declared, including another pointer declarator (which would indicate a pointer to a pointer):

The that appear between  and the identifier (or other nested declarator) qualify the type of the pointer that is being declared:

The is an optional list of, applied to the declared pointer.

Explanation
Pointers are used for indirection, which is a ubiquitous programming technique; they can be used to implement pass-by-reference semantics, to access objects with dynamic, to implement "optional" types (using the null pointer value), aggregation relationship between structs, callbacks (using pointers to functions), generic interfaces (using pointers to void), and much more.

Pointers to objects
A pointer to object can be initialized with the result of the applied to an expression of object type (which may be incomplete):

Pointers may appear as operands to the (unary ), which returns  identifying the pointed-to object:

Pointers to objects of and  type may also appear as the left-hand operands of the  operator.

Because of the implicit conversion, pointer to the first element of an array can be initialized with an expression of array type:

Certain, , operators are defined for pointers to elements of arrays.

are defined for pointers to objects in some situations: two pointers that represent the same address compare equal, two null pointer values compare equal, pointers to elements of the same array compare the same as the array indexes of those elements, and pointers to struct members compare in order of declaration of those members.

Many implementations also provide strict total ordering of pointers of random origin, e.g. if they are implemented as addresses within continuous ("flat") virtual address space.

Pointers to functions
A pointer to function can be initialized with an address of a function. Because of the conversion, the address-of operator is optional:

Unlike functions, pointers to functions are objects and thus can be stored in arrays, copied, assigned, passed to other functions as arguments, etc.

A pointer to function can be used on the left-hand side of the ; this invokes the pointed-to function:

Dereferencing a function pointer yields the function designator for the pointed-to function:

are defined for pointers to functions (they compare equal if pointing to the same function).

Because compatibility of function types ignores top-level qualifiers of the function parameters, pointers to functions whose parameters only differ in their top-level qualifiers are interchangeable:

Pointers to void
Pointer to object of any type can be to pointer to  (optionally  or -qualified), and vice versa:

Pointers to void are used to pass objects of unknown type, which is common in generic interfaces: malloc returns, qsort expects a user-provided callback that accepts two arguments. pthread_create expects a user-provided callback that accepts and returns. In all cases, it is the caller's responsibility to convert the pointer to the correct type before use.

Null pointers
Pointers of every type have a special value known as null pointer value of that type. A pointer whose value is null does not point to an object or a function (dereferencing a null pointer is undefined behavior), and compares equal to all pointers of the same type whose value is also null.

To initialize a pointer to null or to assign the null value to an existing pointer, a null pointer constant (null, or any other integer constant with the value zero) may be used. also initializes pointers to their null values.

Null pointers can indicate the absence of an object or can be used to indicate other types of error conditions. In general, a function that receives a pointer argument almost always needs to check if the value is null and handle that case differently (for example, free does nothing when a null pointer is passed).