Why is not essential to give the size of the first dimension ---eg. int func (int array [] [20], int x));--- when declaring a two dimensional array as a parameter of a function?
Printable View
Why is not essential to give the size of the first dimension ---eg. int func (int array [] [20], int x));--- when declaring a two dimensional array as a parameter of a function?
It just needs to know the length of each row. It doesn't care how many rows you have, that's your job to keep track of. It's the same reason you aren't required to pass the length of a single array:It's actually because it changes arrays to a pointer to their first element's type when it passes them to a function.Code:void foo( int bar[] );
Quzah.
The alternative way of saying it (which also reinforces the "it's a pointer to" idea) is
int func (int (*array)[20], int x);
There is no size (empty or otherwise), it's all the same to the compiler.
You can do this if you want.
Here's a simpler exampleCode:// even if you specify a size, it doesn't make any difference.
int func (int array [5][20], int x);
int main ( ) {
int a[100][20], b[200][20], c[20];
func( a, 100 );
func( b, 200 );
func( &c, 1 );
}
Saying
size_t strlen( char s[100] ); // not char* s
does not limit you to working out the length of strings which are only 100 characters long.