468,272 Members | 2,043 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

using function arguments in other arguments

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
Sep 10 '08 #1
8 1218
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
Sep 10 '08 #2
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
Sep 10 '08 #3
>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
Sep 10 '08 #4
On Sep 10, 1:26*pm, Lynn McGuire <l...@winsim.comwrote:
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

Sep 10 '08 #5
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

Sep 10 '08 #6
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
Sep 10 '08 #7
On Sep 11, 5:57 pm, Lynn McGuire <l...@winsim.comwrote:

[...]
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:ja*********@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
Sep 11 '08 #8
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 we convert our calculation engine to C++, it will
be done all at once. That way we will not have
multiple dimension issues, string issues, etc... The
Fortran to C intermix issues are a great source of
problems. I cannot imagine having many areas of
intermixing from a code maintenance viewpoint.
If you're dealing with mixed Fortran and C++, you definitly want
to read Barton and Nackman.
Thanks, I bought a copy of the book on Amazon.
I have 550,000 lines of F77 code in 3,500 functions,
5,000 lines of C code and 5,000 lines of C++ code in
our calculation engine right now. Plus 350,000 lines
of C++ code in our user interface. We know how to
write Engineering code.

Thanks,
Lynn
Sep 11 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
21 posts views Thread by dragoncoder | last post: by
41 posts views Thread by Telmo Costa | last post: by
29 posts views Thread by shuisheng | last post: by
9 posts views Thread by CryptiqueGuy | last post: by
83 posts views Thread by liketofindoutwhy | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.