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

Dimensioning an array into a structure type

m_santos
Beginner
335 Views

Hello!

I have some problems to declare an array into a structure type.

Thedimensions are passed as an argument of a subroutine.

For example:

Subroutine FSUB(Gn,Cv,Mv,NCdim,NVdim,NPdim)

structure Fortran

real*4 arrayA(NCdim,NVdim)

real*4 arrayB(NVdim,NPdim)

end structure

I receive the following message:

Error: Adjustable, assumed-size, or passed-length declarations are invalid in fielddeclarations. [arrayA]

Can anybody help me?

Thanks in advance.

MSantos

0 Kudos
4 Replies
Steven_L_Intel1
Employee
335 Views

A structure (or defined) type has a fixed size in Fortran. You're asking it to create a type whose size changes depending on run-time values, and that's not doable in Fortran. What you can do is use ALLOCATABLE arrays as the structure elements - these can be allocated to any dimension.

I suggest using the standard Fortran TYPE syntax rather than the STRUCTURE/RECORD extension.

0 Kudos
m_santos
Beginner
335 Views

But, if I use ALLOCATABLE arrays as the structure elements, the structure has a variable size. Its works?

The problem is that the arrayA and arrayB will be determined from arrays passed as argument from C.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
335 Views
m.santos:

But, if I use ALLOCATABLE arrays as the structure elements, the structure has a variable size.

Not really -- an allocatable array within a structure approximately works like a C pointer -- it's a fixed-size reference to data which actually exist elsewhere in memory.

Note that approximately above means it's not the same as C pointer (it's an approx. 8-word descriptor in IVF implementation), and you can't exchange it with C as such (if you'd like to).

Perhaps we could offer some broader perspective suggestions if you could explain more precisely what you're trying to do.

0 Kudos
jimdempseyatthecove
Honored Contributor III
335 Views

I think I understand your problem andI have a suggestion. The C program is passing you the address of a structure with one or more variable length sub-structures within the C structure. The suggestion I have is to create a structure reference structure the purpose of which is to map the C structure in a manner acceptible to Fortran

type CFixedData
! define the fixed data layout
end type CFixedData

type CRefStruct
type(CFixedData), pointer :: pFixed
integer, pointer :: pVariable(:)
end type cRefStruct

type(CRefStruct) :: aCRefStruct

Then upon receipt of the C structure pointer, initialize the aCRefStruct.

You can extend this such that if the variable data consists of a small number of different sub structures then you can define the C structure using a UNION with the different sub structures

type Cstructure
sequence
! define fixed portion
union
map
! define 1st variant
end map
map
! define 2nd variant
end map
! more map, defines, end map
end union
end type Cstructure

Jim Dempsey

0 Kudos
Reply