Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Array Of Pointers

akochhar
Beginner
432 Views
Hi,
I'm trying to pass a 4 byte array of pointers from my fortran 90 program to a C++ DLL.
However I'm having trouble just setting up an array of pointers in fortran.
I've used the flowing code:

REAL*4 PtrArray(4)

REAL*4, TARGET :: a(8)
REAL*4, TARGET :: b(5)
REAL*4, TARGET :: c((150*100+2),2)
REAL*4, TARGET :: d(6,150*100+2)

REAL, POINTER :: aptr
REAL, POINTER :: bptr
REAL, POINTER :: cptr
REAL, POINTER :: dptr

a(:) = 1.0
b(:) = 2.0
c(:,:) = 3.0
d(:,:) = 4.0

aptr=>a
bptr=> b
cptr => c
dptr => d

PtrArray(1) = aptr
PtrArray(2) = bptr
PtrArray(3) = cptr
PtrArray(4) = dptr

C:PointerTestPointerPOINTER2.F90
C:PointerTestPointerPOINTER2.F90(60) : Error: The rank of the target is different from the pointer [aPTR]
aptr=>a
----------------^
C:PointerTestPointerPOINTER2.F90(61) : Error: The rank of the target is different from the pointer [bPTR]
bptr=> b
----------------^
C:PointerTestPointerPOINTER2.F90(62) : Error: The rank of the target is different from the pointer [cPTR]
cptr => c
----------------^


C:PointerTestPointerPOINTER2.F90(63) : Error: The rank of the target is different from the pointer [MPPTR]
dptr => d

I then triedto rectify the problem by changing the code to the following:

REAL*4 , pointer:: PtrArray(4)

REAL*4, TARGET :: a(8)
REAL*4, TARGET :: b(5)
REAL*4, TARGET :: c((150*100+2),2)
REAL*4, TARGET :: d(6,150*100+2)
ALLOCATE (PtrArray(4))

a(:) = 1.0
b(:) = 2.0
c(:,:) = 3.0
d(:,:) = 4.0

PtrArray(1) = a
PtrArray(2) = b
PtrArray(3) = c
PtrArray(4) = d

However this seems to yield more errors:
--------------------Configuration: TestPointer - Win32 Debug--------------------
Compiling Fortran...
C:PointerTestPointerPOINTER2.F90
C:PointerTestPointerPOINTER2.F90(66) : Error: The shapes of the array expressions do not conform. [PTRARRAY]
PtrArray(1) = a
--------^
C:PointerTestPointerPOINTER2.F90(67) : Error: The shapes of the array expressions do not conform. [PTRARRAY]
PtrArray(2) = b
--------^
C:PointerTestPointerPOINTER2.F90(68) : Error: This name does not have a type, and must have an explicit type. [LKB]
PtrArray(3) = c
----------------------^
C:PointerTestPointerPOINTER2.F90(69) : Error: The shapes of the array expressions do not conform. [PTRARRAY]
PtrArray(4) = d
--------^
Error executing df.exe.

It seems that Fortran is expecting the PtrArray to bedeclared as an array of arrays. However this doesn't seem possible b/c each element of the PtrArray would have to declared tohold a different size and dimension array since my arrays a,b,c,d are of differentsizes.

Really all I am looking to do is create an array which will hold the addresses of the a,b,c and d arrays.

Can anyone show mehow to do this. I haven't been able to find info on this in the fortran help documentation, or fortran books.

Many Thanks,

0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
432 Views
As was said on comp.lang.fortran, Fortran pointers are not the same as C pointers.

You can get the address of an array using non-standard LOC() function, which returns the address of its argument (i.e. the same as C's & operator). The PtrArray must be an INTEGER array, or, better, INTEGER(INT_PTR_KIND()) array (INT_PTR_KIND returns the integer kind sufficient to hold an address):
INTEGER(INT_PTR_KIND()):: PtrArray(4)
...
PtrArray(1) = LOC(a)
PtrArray(2) = LOC(b)
PtrArray(3) = LOC(c)
PtrArray(4) = LOC(d)
Note that you cannot dereference the PtrArray back in Fortran (except using other cludges/extensions, such as "Cray pointers").

See also this recent thread.

Jugoslav
0 Kudos
Reply