473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1405
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
6
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.