James Kanze
9/11/2008 8:15:00 AM
On Sep 10, 11:03 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Lynn McGuire wrote:
> > Can I use a function argument as an array limit in a
> > another argument ? Such as:
> > int myFunction (int ncp, double myArray [ncp] [2]);
> > and
> > int myFunction (int * ncp, double myArray [*ncp] [2]);
> No. That would attempt to declare a function that takes
> different types as the second argument, and in C++ that's not
> really possible, it's a statically typed language.
Not really. The syntax requires that the dimension be a
constant integral expression or nothing; since ncp and *ncp are
not constant integral expressions, the declarations are illegal.
But it really doesn't matter, since the first dimension is
ignored anyway:
int myFunction( double array[ 1200 ] ) ;
and
int myFunction( double array[ 2 ] ) ;
declare the same function, with an actual signature of
int myFunction( double* ) ;
Without the syntax violation, his functions have the
signature:
int myFunction( int ncp, double (*myArray)[ 2 ] ) ;
int myFunction( int ncp*, double (*myArray)[ 2 ] ) ;
I'm not sure what he's trying to do, but since he mentions
Fortran, it might be worth pointing out that arrays in Fortran
are column major, in C row major. This means that in order to
calculate the index into a two dimensional array, the Fortran
compiler needs to know the first dimension, the C++ compiler the
second. In his example, the second dimension was a constant, so
no problem. In general, however, C++ (unlike Fortran, if memory
serves me correctly, and unlike C) has no support for dimensions
which are not constants.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34