[Haskell-cafe] Specify array or list size?

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat May 7 16:09:16 EDT 2005


Hamilton Richards <ham at cs.utexas.edu> writes:

> That's not the case in C, C++, Java, or Ada. In C and C++, for
> example, given two arrays
>
> 	int X[50];
> 	int Y[100];
>
> and a function declared as
>
> 	void P( int a[] )
>
> then these calls
>
> 	P( X )
> 	P( Y )
>
> are both valid, because the sizes of X and Y are not part of their type.

But here you don't pass the array but a pointer to its first element.
You can even call P(&x) where x is an int, not an array at all.

Consider this:

int X[50];
int Y[100];
void P(int (&a)[50]) {}
int main() {
   P(X); // valid
   P(Y); // invalid
}

> In C and C++, there's not even any way for a function to discover
> the size of an array argument.

template<int N>
int size(int (&a)[N]) {
    return N;
}

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


More information about the Haskell-Cafe mailing list