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 8 1390
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
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
>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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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()',...
|
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...
|
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...
|
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;
}
|
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 +=...
|
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...
|
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...
|
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...
|
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...);...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |