2

Many people say compiler would not know the size information if no column size is provided. But How compiler knows the row size? And why column size can't be interpreted?

Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
nathan
  • 754
  • 1
  • 10
  • 24

1 Answers1

2

While passing an array as an argument to a function, compiler implicitly converts the array reference to the pointer. This can be clearly seen in the case of one dimensional array, like

    int method(int *a)
    int method(int a[])

Both these lines are equivalent here (although pointer and array reference are different) because whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0]. However this rule is not recursive i.e. passing a 2d array is treated as pointer to an array, not a pointer to a pointer.

    int method(int b[3][5]);

is converted to

    int method(int (*b)[5]);

And since the called function doesn't allocate space for the array so size of row is not important, however size of column is still important to provide the size of array. For further reference you can visit here.

  • Hi,Rajeev. Thanks for you good post and reference. That really gave something new to me. However, I am still unclear with one sentence:"Since the called function does not allocate space for the array, it does not need to know the overall size, so the number of rows, NROWS, can be omitted. The width of the array is still important" – nathan Jun 24 '16 at 15:24
  • Just remember a 2d array is implicitly array of pointers, and as you know for every array size must be mentioned, and here the size is column size .@user3529352 – Rajeev Ranjan Jun 25 '16 at 12:27