[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

using function arguments in other arguments

Lynn McGuire

9/10/2008 8:26:00 PM

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]);

Thanks,
Lynn
21 Answers

utab

9/10/2008 9:03:00 PM

0

On Wed, 10 Sep 2008 15:26:23 -0500, 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]);
>
> Thanks,
> Lynn

This is the way you pass arrays to a function, passing the name of the
array(while array name is automatically converted to a pointer to the
first element during the function call) and the size of the array, but
what is your aim with these prototypes? Better suggestions can come out
if readers know the ultimate goal, my humble idea...
best

Victor Bazarov

9/10/2008 9:04:00 PM

0

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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Lynn McGuire

9/10/2008 9:53:00 PM

0

>> int myFunction (int ncp, double myArray [ncp] [2]);

> This is the way you pass arrays to a function, passing the name of the
> array(while array name is automatically converted to a pointer to the
> first element during the function call) and the size of the array, but
> what is your aim with these prototypes? Better suggestions can come out
> if readers know the ultimate goal, my humble idea...

We do this all the time in fortran. My intent here
is to know the size of the array being passed to the
function from the caller. Otherwise, my function does
not have a clue as to the first limit of the array.

Thanks,
Lynn

Greg Herlihy

9/10/2008 9:54:00 PM

0

On Sep 10, 1:26 pm, Lynn McGuire <l...@winsim.com> 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]);

Since array bounds are compile-time constants in C++, it would be
possible to implement a function template that would take an array
limit from one function parameter and pass that value on - as an
argument to another function. For example:

#include <iostream>

void f(int n)
{
std::cout << "N: " << n << "\n";
}

template <int N>
int myFunction (int ncp, double (&myArray)[N][2])
{
f(N);
}

double a[7][2];

int main()
{
myFunction(5, a);
}

Program Output:

N: 7

Greg

utab

9/10/2008 10:49:00 PM

0

On Wed, 10 Sep 2008 16:53:12 -0500, Lynn McGuire wrote:
> We do this all the time in fortran. My intent here is to know the size
> of the array being passed to the function from the caller. Otherwise,
> my function does not have a clue as to the first limit of the array.
>
> Thanks,
> Lynn

Hmm, the below Fortran code is out of scope of this group but that is
completely for example purposes, but I could not understand what you
meant above, as far as I know, in C/C++ there is no direct way to get the
array size from within the function that is why you need to specify the
size as an argument. But in fortran you can get that from inside the
function, as below... Did I correctly understand you?

program array_pass
real, dimension(4,5) :: yourArray
call printDims(yourArray)
contains
subroutine printDims(array)
! deferred shape
real, dimension(:,:) :: array
write(*,*) size(array,1), size(array,2)
end subroutine printDims
end program array_pass

12:44 AM utabak@dutw689 ~ $ ifort arr.f90
12:44 AM utabak@dutw689 ~ $ ./a.out
4 5

Lynn McGuire

9/10/2008 11:49:00 PM

0

> Hmm, the below Fortran code is out of scope of this group but that is
> completely for example purposes, but I could not understand what you
> meant above, as far as I know, in C/C++ there is no direct way to get the
> array size from within the function that is why you need to specify the
> size as an argument. But in fortran you can get that from inside the
> function, as below... Did I correctly understand you?

Nope. Here is my example in F77.

subroutine myFunction (ncp, myArray)
integer ncp
double precision myArray (ncp, 2)
...
return
end

All I want to know is how to duplicate this functionality
in C++.

Lynn

Alf P. Steinbach

9/11/2008 12:29:00 AM

0

* Lynn McGuire:
>>> int myFunction (int ncp, double myArray [ncp] [2]);
>
>> This is the way you pass arrays to a function, passing the name of the
>> array(while array name is automatically converted to a pointer to the
>> first element during the function call) and the size of the array, but
>> what is your aim with these prototypes? Better suggestions can come
>> out if readers know the ultimate goal, my humble idea...
>
> We do this all the time in fortran. My intent here
> is to know the size of the array being passed to the
> function from the caller. Otherwise, my function does
> not have a clue as to the first limit of the array.

In C++ you have essentially two choices: that the array size is known at compile
time, or that it's only known at run time.

A function that works with the caller having run time knowledge of array size
can also work with caller having compile time knowledge (obviously), but you can
also devise a function that requires caller to have compile time knowledge.

To show the latter first:

template< size_t N >
void foo( double const (&myArray)[N] )
{
for( size_t i = 0; i < N; ++i ) { bar( myArray[i] ); }
}

int main()
{
static double const a[] = {1.0, 2.0, 3.0};
foo( a );
}

For run-time size you'd use std::vector or some other library class,

void foo( std::vector<double> const& myArray )
{
for( size_t i = 0; i < myArray.size(); ++i ) { bar( myArray[i] ); }
}

int main( int n, char** )
{
static std::vector<double> a( n );
foo( a );
}

Any decent C++ textbook will explain this.

It's probably a good idea to invest in a textbook.


Cheers & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

James Kanze

9/11/2008 8:15:00 AM

0

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

Lynn McGuire

9/11/2008 3:57:00 PM

0

> I'm not sure what he's trying to do, but since he mentions

If you give the array length(s), our debugger will
automatically open the entire array for you to peruse
at your leisure. Otherwise, it is a real pain to look
at individual members of the arrayed variable. We
currently use the Open Watcom F77 and C++ compilers on
Win32.

> Fortran, it might be worth pointing out that arrays in Fortran
> are column major, in C row major. This means that in order to

Argh ! I forgot that ! One of these days we are
contemplating converting our f77 code to C++ code.
The means all multiple dimension variables will be
a disaster (and no, we are not going to do this by
hand, we will use http://www.cobalt-blue.com/fc/... ).

> 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.

Yes, F77 does allow you use other variables as array
dimensions. Fortran allows you to use the following
code just fine:

subroutine myFunction (ncp, myArray)
integer ncp
double precision myArray (ncp, 2)
...
return
end

Looks like I have finally found a serious problem with
converting our F77 code to C++.

Thanks,
Lynn

James Kanze

9/11/2008 9:25:00 PM

0

On Sep 11, 5:57 pm, Lynn McGuire <l...@winsim.com> wrote:

[...]
> > 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.

> Yes, F77 does allow you use other variables as array
> dimensions. Fortran allows you to use the following code
> just fine:

> subroutine myFunction (ncp, myArray)
> integer ncp
> double precision myArray (ncp, 2)
> ...
> return
> end

> Looks like I have finally found a serious problem with
> converting our F77 code to C++.

The usual solution is to define a class for arrays of 2 (or
more) dimensions, which hides all of the differences. It's
even possible to implement the class so that the array is column
major. And with a bit of foresight, so that it converts to
something that Fortran thinks is a multiple dimension array.

If you're dealing with mixed Fortran and C++, you definitly want
to read Barton and Nackman.

--
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