Search code examples
cpointersmultidimensional-arraypointer-to-pointer

C, Dynamic allocation of a matrix: Why is this not allowed?


So I have the following example in some lecture notes

void f(int **p){}
void g(int *p[]){}
void h(int p[2][3]){}

int main(){
   int **a;
   allocate_mem(a); // allocate memory for a
   f(a); // OK!
   g(a); // OK!
   // h(a); // NOT OK
   int b[2][3];
   // f(b); // NOT OK
   // g(b); // NOT OK
   h(b); // OK!
   return 0;
}

(without any further explanation/comments). I am struggling to understand exactly why f(b) and g(b) would be illegal. Both of these functions are designed to accept two-dimensional arrays, and we are calling them with one. How does that not work? I assume the difference lies in the allocation of memory, but how would that affect how a function accepts it as input?


Solution

  • You're conflating pointers with arrays, and pointers-to-pointers with two-dimensional arrays.

    That's an understandable mistake, due to C (and C++)'s "array-to-pointer decay". Sometimes you can refer to an array, and get a pointer to its first element; sometime it's the actual array - depends on the context. And with two-dimensional arrays it gets even weirder, since a 2-dimensional arrays can be used in less places instead of pointer-to-pointer-to an element (but can still be used in some places like that).

    Please spend a few minutes reading about how pointers and arrays relate and differ, in Section 6 of the C language FAQ. Your specific question appears there as well:

    Q 6.18: "My compiler complained when I passed a two-dimensional array to a function expecting a pointer to a pointer."