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

Unlimited polymorphic and arrays

evanscpi_com
Beginner
1,166 Views
I'm not quite sure how to interpret the Fortran standard regarding use of arrays as unlimited polymorphic entities. I have a prcedure that accepts an unlimited polymorphic entity and I want to pass an array of real values. But when I try to assign a pointer to the array I get and error. Here's some sample code that illustrates the problem:

real(kind=kind(1.0)), dimension(3), target :: real_array
class(*), pointer :: real_array_ptr
real_array = [1.0, 2.0, 3.0]
real_array_ptr => real_array

The error is:

test.f(18): error #6794: The rank of the target is different from the pointer   [REAL_ARRAY_PTR]
  real_array_ptr => real_array
--^

Is a real array object type compatible with an unlimited polymorpic object, and if so, how should the code be written to handle this?

ifort -V
Intel® Fortran Intel® 64 Compiler XE for applications running on Intel® 64, Version 12.1.5.344 Build 20120612

Xcode version: 3.2.6

Mac OS X version 10.6.8
0 Kudos
4 Replies
Steven_L_Intel1
Employee
1,166 Views
class(*) will match any type and kind, but you still are required to match rank (unless the called routine is ELEMENTAL).  It is not a "C void *" type of thing. 
0 Kudos
evanscpi_com
Beginner
1,166 Views
Hi Steve, thanks for your response. I think I'm missing something very basic here. The error message I reported is occuring before the call to the procedure. The pointer assignment of class(*) to real array is the source of the error. I apologize for what may be a basic question, but how do I assign a class(*) pointer to a real array (i.e. such that the objects have equal rank)? Thanks!
0 Kudos
IanH
Honored Contributor III
1,166 Views
When you declare the pointer, specify that it points to a rank one array:

[fortran]class(*), pointer :: array(:)   ! Note trailing (:)[/fortran]
0 Kudos
evanscpi_com
Beginner
1,165 Views
Thanks, Steve. That change does fix the rank mismatch problem.



0 Kudos
Reply