Unlimited polymorphic and arrays

Unlimited polymorphic and arrays

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

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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. 

Steve - Intel Developer Support

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!

When you declare the pointer, specify that it points to a rank one array:

class(*), pointer :: array(:)   ! Note trailing (:)

Thanks, Steve. That change does fix the rank mismatch problem.

Leave a Comment

Please sign in to add a comment. Not a member? Join today