-2

I am struck while reading 2D array in C++ that we can declare in
Such a way:

month=4;.   // Initialize value of mont variable
void display(float [ ] [month] );   //declare

I want to know why the function doesn't need the size of fist dimension ?

I ask this question on many forums but only get the way how to declare array like this . But never find the answer of why?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Abhay
  • 19
  • 1
  • 3

1 Answers1

0

Because arrays when passed to functions are treated like pointers (to the arrays first element).

So an argument declaration like

float month[][X]

is equal to

float (*month)[X]

So month is a pointer to an array of X elements of type float.

This is because a "2d" array is really an array of arrays. C++ doesn't really have multi-dimensional arrays.

Also note that an array of arrays is not the same as a pointer to a pointer. The decay to a pointer only happens for the outer array ("first dimension").

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621