0

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?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
HereBeeBees
  • 145
  • 9
  • 1
    `f` and `g` accept a pointer to a pointer to `int`, not arrays. – melpomene Apr 22 '18 at 13:19
  • `allocate_mem` is not standard C. Use [malloc](http://en.cppreference.com/w/c/memory) or similar – Basile Starynkevitch Apr 22 '18 at 13:19
  • 2
    Possible duplicate: https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays – StoryTeller - Unslander Monica Apr 22 '18 at 13:20
  • @melpomene I was under the impression that arrays where simply pointers to the first element, but after reading the post linked to by StoryTeller it seems that they are not strictly the same, and will be considered different data types? – HereBeeBees Apr 22 '18 at 13:27
  • 3
    @HereBeeBees Arrays and pointers are not even close to the same. They have nothing in common; it's just that arrays can be implicitly converted to pointers (and this happens in most contexts). – melpomene Apr 22 '18 at 13:35

1 Answers1

2

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."

einpoklum
  • 118,144
  • 57
  • 340
  • 684