Triple pointers in C: is it a matter of style?

Using triple+ pointers is harming both readability and maintainability.

Let’s suppose you have a little function declaration here:

void fun(int***);

Hmmm. Is the argument a three-dimensional jagged array, or pointer to two-dimensional jagged array, or pointer to pointer to array (as in, function allocates an array and assigns a pointer to int within a function)

Let’s compare this to:

void fun(IntMatrix*);

Surely you can use triple pointers to int to operate on matrices. But that’s not what they are. The fact that they’re implemented here as triple pointers is irrelevant to the user.

Complicated data structures should be encapsulated. This is one of manifest ideas of Object Oriented Programming. Even in C, you can apply this principle to some extent. Wrap the data structure in a struct (or, very common in C, using “handles”, that is, pointers to incomplete type – this idiom will be explained later in the answer).

Let’s suppose that you implemented the matrices as jagged arrays of double. Compared to contiguous 2D arrays, they are worse when iterating over them (as they don’t belong to a single block of contiguous memory) but allow for accessing with array notation and each row can have different size.

So now the problem is you can’t change representations now, as the usage of pointers is hard-wired over user code, and now you’re stuck with inferior implementation.

This wouldn’t be even a problem if you encapsulated it in a struct.

typedef struct Matrix_
{
    double** data;
} Matrix;

double get_element(Matrix* m, int i, int j)
{
    return m->data[i][j];
}

simply gets changed to

typedef struct Matrix_
{
    int width;
    double data[]; //C99 flexible array member
} Matrix;

double get_element(Matrix* m, int i, int j)
{
    return m->data[i*m->width+j];
}

The handle technique works like this: in the header file, you declare a incomplete struct and all the functions that work on the pointer to the struct:

// struct declaration with no body. 
struct Matrix_;
// optional: allow people to declare the matrix with Matrix* instead of struct Matrix*
typedef struct Matrix_ Matrix;

Matrix* create_matrix(int w, int h);
void destroy_matrix(Matrix* m);
double get_element(Matrix* m, int i, int j);
double set_element(Matrix* m, double value, int i, int j);

in the source file you declare the actual struct and define all the functions:

typedef struct Matrix_
{
    int width;
    double data[]; //C99 flexible array member
} Matrix;

double get_element(Matrix* m, int i, int j)
{
    return m->data[i*m->width+j];
}

/* definition of the rest of the functions */

The rest of the world doesn’t know what does the struct Matrix_ contain and it doesn’t know the size of it. This means users can’t declare the values directly, but only by using pointer to Matrix and the create_matrix function. However, the fact that the user doesn’t know the size means the user doesn’t depend on it – which means we can remove or add members to struct Matrix_ at will.

Leave a Comment