I have a problem declaring a static 2D array, because the parameters that I use to size it are passed from a Fortran function (they must come from there). Therefore, they are not recongnized as constant numbers. Is there any way around that?
why 2D ?
i suppose you get from fortran a MAX int||long value
try it dynamically using malloc && realloc on a pointer
of the type you will use. -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
The reason for 2D is because it is a geometry file, so it has to be 2D. I've had problems passing a 2D array from Fortran to C, so I copied a 2D array into 1D and passed it to C that way, but I can't copy it back into 2D (it's a necessary part of the program).... I am sorry, I didn't really understand what you said, I am not a good programmer (actually, an engineer)....
I'd guess the reason you have problems passing the array is that FORTRAN stores 2D arrays in column-major order, while C et. al. store them in row-major order. That 1D array is probably the best way around that particular problem.
I'd go with jamisar's suggestion to use dynamic memory allocation. You use the malloc (memory allocation) function to get the amount of memory you need at run time.
For example, you if you want to copy a 4 x 4 array of ints, you'd allocate enough space for 4 pointers to int:
[tt]int **grid = malloc( 4, sizeof( int * ) );[/tt]
Then you'd go through the array of pointers and similarly allocate space for 4 ints in each of those.
After you're done with the array, go through and free everything you dynamically allocated by using the free function.
you can use an array of (n,n) dimensions like a unidimensional array (or pointer)if you use pseudoindices, considering that an array ocuppies adyacent positions in memory, according fortran convention.
If i see the source i could help you
This is a subroutine to get inverse of a matrix, passing an array form visual basic, it was in fortran. if i do not understand you have a problem like this. i think it can help you.
#include <windows.h>
#include <math.h>
void __stdcall matinv( int n, double *a_, double *ai_);
BOOL WINAPI DllMain( HINSTANCE hinstDLL ,DWORD dwReason ,LPVOID lpReserved )
{
// Perform actions based on the reason for calling.
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE;
}
void __stdcall matinv( int n, double *a_, double *ai_)
{
double *a=(double *)a_ /* [n*n] elements */;
double *ai=(double *)ai_ /* [n*n] elements */;
int i,j,k;
double t,ti;
This makes sure the cells are contiguous, which is what Fortan wants. Pass C2F to the Fortran routine. Fortran doesn't know about C type 2D arrays.
When you are accessing it in C, put on an additional layer.
#define cell(r,c) C2F[r*cmax+c]
This is similar to the technique used by bytebit. It is far easier than two lots of [][]. There is no end of problems using dynamic 2D arrays in C because routines only expect one dimension to be dynamic.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.