2

If suppose I have two arrays arr a 1D array and brr a 2D array and I want to pass these arrays as input to functions

In case of a 1D :

void fun(int*); OR void fun(int[]); //both declarations are allowed

fun(arr);//call for 1D

void fun(int *ptr)
{
 //function def of 1D
}

But in case of a 2D Array:

void fun(int[][]); //allowed
void fun(int**);  //this is not allowed **WHY so?**

While the call and definitions are similar to the previous 1D array

fun(brr);//call

void fun(int **ptr2)
{
 //code
}
Ron Axm
  • 101
  • 9

1 Answers1

1

This is because by convention a pointer to an array is considered as a qualified pointer to the first element of type in many occasions especially on function passed parameters.

You can consult the ISO/IEC 9899:2011 § 6.7.6.3 Function declarators (including prototypes) - Semantics:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.

But a declaration of type int[][] instead of int**is not interchangeable because while the first refers to a 2D array, and it will be silently changed by the compiler to a pointer to the first element of the 2D array, the second means a pointer to pointer of int, that is not compatible with a 2D array.

Sometime people are induced in confusion by pseudo 2D arrays defined dynamically as follows:

int **My_2D_array = malloc(sizeof(int *) * 2);  //We are allocating 2 pointers to int
My_2D_array[0] = malloc(sizeof(int)*2);  //We allocate the first dimension space
My_2D_array[1] = malloc(sizeof(int)*2);  //We allocate the second dimension space

In this case the name My_2D_array will be referred as the address of the first element of an array of 2 pointers to int, and the use of the [] operator will return the first pointer in the pointers array. Applying again the operator [] onto the pointer just obtained, and again used as the address of the first element of a monodimensional array of int's, will returs an int:

int my_int = My_2D_array[0][1];  //mimics a bidimensional array, but isn't!
Frankie_C
  • 4,764
  • 1
  • 13
  • 30