TakaTiki TakaTiki - 4 years ago 83
C Question

Differences when using ** in C

I started learning C recently, and I'm having a problem understanding pointer syntax, for example when I write the following line:

int ** arr = NULL;

How can I know if:

  • arr is a pointer to a pointer of an integer

  • arr is a pointer to an array of pointers to integers

  • arr is a pointer to an array of pointers to arrays of integers

Isn't it all the same with
int **


Another question for the same problem:

If I have a function that receives
char ** s
as a parameter, I want to refer to it as a
to an array of strings, meaning a pointer to an array of pointers to an array of
, but is it also a pointer to a pointer to a

Answer Source

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;


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


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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download