Differences when using ** in C

Isn’t it all the same with int **?

You’ve just discovered what may be considered a flaw in the type system. Every option you specified can be true. It’s essentially derived from a flat view of a programs memory, where a single address can be used to reference various logical memory layouts.

The way C programmers have been dealing with this since C’s inception, is by putting a convention in place. Such as demanding size parameter(s) for functions that accept such pointers, and documenting their assumptions about the memory layout. Or demanding that arrays be terminated with a special value, thus allowing “jagged” buffers of pointers to buffers.


I feel a certain amount of clarification is in order. As you’d see when consulting the other very good answers here, arrays are most definitely not pointers. They do however decay into ones in enough contexts to warrant a decades long error in teaching about them (but I digress).

What I originally wrote refers to code as follows:

void func(int **p_buff)
{
}

//...

int a = 0, *pa = &a;
func(&pa);

//...

int a[3][10];
int *a_pts[3] = { a[0], a[1], a[2] };
func(a_pts);

//...

int **a = malloc(10 * sizeof *a);
for(int i = 0; i < 10; ++i)
  a[i] = malloc(i * sizeof *a[i]);
func(a);

Assume func and each code snippet is compiled in a separate translation unit. Each example (barring any typos by me) is valid C. The arrays will decay into a “pointer-to-a-pointer” when passed as arguments. How is the definition of func to know what exactly it was passed from the type of its parameter alone!? The answer is that it cannot. The static type of p_buff is int**, but it still allows func to indirectly access (parts of) objects with vastly different effective types.

Leave a Comment