How To Access 3/4 Dimensional Arrays Using Pointers
Author Message
How To Access 3/4 Dimensional Arrays Using Pointers

Hello,

Can you tell me (with good examples) how to access 3 and 4 dimensional
fortran arrays using pointers in C/C++?  My program uses two languages.  I have
arrays defined in Fortran. In C I am given the base address of the Fortran
arrays. How do I get the address of individual elements

EG: The following is the lower half of the addresses of a three dimensional
array.

Fortran declaration
int_tes (3,2,2)

int_tes (1,1,1) = 548
int_tes (1,1,2) = 656
int_tes (1,2,1) = 602
int_tes (1,2,2) = 710
int_tes (2,1,1) = 566
int_tes (2,1,2) = 674
int_tes (2,2,1) = 620
int_tes (2,2,2) = 728
int_tes (3,1,1) = 584
int_tes (3,1,2) = 692
int_tes (3,2,1) = 638
int_tes (3,2,2) = 746

548 + 108 = 656 to go from element 1,1,1 to 1,1,2
548 + 18 = 566  to go from element 1,1,1 to 2,1,1
548 + 54 = 602  to go from element 1,1,1 to 1,2,1

The size of an integer is 3 bytes.

(1,,)                (2,,)                (3,,)
548   656            566    674           584   692
602   710            620    728           638   746

Thank you,
Christopher Lusardi

Tue, 18 Jul 2006 04:12:41 GMT
How To Access 3/4 Dimensional Arrays Using Pointers

Quote:

> Hello,

>    Can you tell me (with good examples) how to access 3 and 4 dimensional
> Fortran arrays using pointers in C/C++?  My program uses two languages.  I
> have arrays defined in Fortran. In C I am given the base address of the
> Fortran arrays. How do I get the address of individual elements

Why don't you pass the array element whenever C++ expects a pointer?
It hasn't to be the base element has it?

/Sakis

Quote:

> EG: The following is the lower half of the addresses of a three
> dimensional array.

> Fortran declaration
> int_tes (3,2,2)

> int_tes (1,1,1) = 548
> int_tes (1,1,2) = 656
> int_tes (1,2,1) = 602
> int_tes (1,2,2) = 710
> int_tes (2,1,1) = 566
> int_tes (2,1,2) = 674
> int_tes (2,2,1) = 620
> int_tes (2,2,2) = 728
> int_tes (3,1,1) = 584
> int_tes (3,1,2) = 692
> int_tes (3,2,1) = 638
> int_tes (3,2,2) = 746

> 548 + 108 = 656 to go from element 1,1,1 to 1,1,2
> 548 + 18 = 566  to go from element 1,1,1 to 2,1,1
> 548 + 54 = 602  to go from element 1,1,1 to 1,2,1

> The size of an integer is 3 bytes.

> (1,,)                (2,,)                (3,,)
> 548   656            566    674           584   692
> 602   710            620    728           638   746

>                    Thank you,
>                    Christopher Lusardi

Tue, 18 Jul 2006 05:44:58 GMT
How To Access 3/4 Dimensional Arrays Using Pointers

Quote:

>    Can you tell me (with good examples) how to access 3 and 4 dimensional
> Fortran arrays using pointers in C/C++?  My program uses two languages.  I have
> arrays defined in Fortran. In C I am given the base address of the Fortran
> arrays. How do I get the address of individual elements

Well, it depends a little on the system you are using.  Assuming that
you have the ability to call between Fortran and C...

Quote:
> EG: The following is the lower half of the addresses of a three dimensional
> array.

> Fortran declaration
> int_tes (3,2,2)

For arrays with constant dimensions, it should be pretty easy.

In the called subroutine declare the array

int int_tes[2][2][3];

and access it as a normal C array.

Quote:
> int_tes (1,1,1) = 548
> int_tes (1,1,2) = 656
> int_tes (1,2,1) = 602
> int_tes (1,2,2) = 710
> int_tes (2,1,1) = 566
> int_tes (2,1,2) = 674
> int_tes (2,2,1) = 620
> int_tes (2,2,2) = 728
> int_tes (3,1,1) = 584
> int_tes (3,1,2) = 692
> int_tes (3,2,1) = 638
> int_tes (3,2,2) = 746

> 548 + 108 = 656 to go from element 1,1,1 to 1,1,2
> 548 + 18 = 566  to go from element 1,1,1 to 2,1,1
> 548 + 54 = 602  to go from element 1,1,1 to 1,2,1

They would look a little better like:

int_tes (1,1,1) = 548
int_tes (2,1,1) = 566
int_tes (3,1,1) = 584
int_tes (1,2,1) = 602
int_tes (2,2,1) = 620
int_tes (3,2,1) = 638
int_tes (1,1,2) = 656
int_tes (2,1,2) = 674
int_tes (3,1,2) = 692
int_tes (1,2,2) = 710
int_tes (2,2,2) = 728
int_tes (3,2,2) = 746

Quote:
> The size of an integer is 3 bytes.

Three byte integers are a little unusual.  Does your C compiler
support them, too?  If so, and you declare it in C as

int int_tes[2][2][3];

It should work fine, though you have to reverse the
order of all the subscripts, and remember that C arrays
are 0 origin instead of 1 origin.

If you allocate the array dynamically, where the dimensions
are not constant, then I believe you must manually calculate
the position of the appropriate element in the array.

int xyz(a,i,j,k,l,m,n)
int *a,i,j,k,l,m,n;
{
/* print Fortran element a(i,j,k) from array
dimensioned a(l,m,n)  */
printf("%d\n",a[(i-1)+(j-1)*l+(k-1)*l*m]);

Quote:
}

By the way, what machine has three byte integers?

-- glen

Tue, 18 Jul 2006 06:40:13 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages