Advanced Pointers

Article · 3:00

Null, void, wild and dangling; pointers to pointers; arrays of pointers; and pointers to functions.

Four kinds of pointer go wrong and it is worth naming them separately. A null pointer points deliberately at nothing, and dereferencing it reliably faults, which makes it the safe way to say "no value". A wild pointer is uninitialised and holds garbage - unpredictable, and the reason to initialise to NULL. A dangling pointer still holds an address whose memory has been freed or whose frame has returned; it looks valid and is not, and setting a pointer to NULL immediately after free turns a silent corruption into a clean crash. A void pointer holds an address with no type, which is how malloc and generic functions work - it cannot be dereferenced until it is cast back.

A pointer to a pointer - int ** - is how a function changes a caller's pointer rather than the thing it points at. Since everything is passed by value, a function that must allocate and hand back memory through a parameter needs the address of the caller's pointer.

An array of pointers is what char *argv[] is: a run of addresses, each pointing at a string that lives elsewhere. Distinguish it from a pointer to an array, which is a different type with different arithmetic.

A function pointer holds the address of code, and is how C does callbacks, plugin tables and state machines - qsort takes one to decide ordering. The declaration syntax is famously awkward; typedef makes it readable, and modern C can take the address without the &.

#include <stdio.h>
#include <stdlib.h>

/* ** so the function can change the CALLER's pointer, not just a copy. */
int allocate(int **out, size_t n)
{
    int *block = malloc(n * sizeof *block);
    if (!block) return -1;
    for (size_t i = 0; i < n; i++) block[i] = (int) i * i;
    *out = block;                 /* writes through to the caller */
    return 0;
}

/* A function pointer, made readable by a typedef. */
typedef int (*comparator)(const void *, const void *);

static int ascending(const void *a, const void *b)
{
    int left = *(const int *) a, right = *(const int *) b;
    return (left > right) - (left < right);    /* no overflow */
}

int main(void)
{
    int *numbers = NULL;          /* NULL, not wild */
    if (allocate(&numbers, 5) != 0) return 1;

    /* qsort takes a void * and a function pointer - this is how C does
       generics and callbacks at the same time. */
    comparator cmp = ascending;   /* no & needed */
    qsort(numbers, 5, sizeof *numbers, cmp);

    for (size_t i = 0; i < 5; i++) printf("%d ", numbers[i]);
    printf("\n");

    free(numbers);
    numbers = NULL;               /* now a second free is a no-op, and a
                                     dereference faults instead of
                                     silently reading freed memory */

    /* void *: an address with no type. Cannot be dereferenced until it
       is cast back to something. */
    void *anything = &cmp;
    printf("%p\n", anything);

    /* An array OF pointers - each element is an address. argv is one. */
    const char *names[] = {"ada", "grace", "alan"};
    for (size_t i = 0; i < 3; i++) printf("%s ", names[i]);
    printf("\n");
    return 0;
}