views:

63

answers:

2

I have a user-defined type vector. In another type, I have an allocatable array of vectors. I want to have a pointer to a single vector from this allocatable array. So I thought I would do this:

type(vector),allocatable,target::my_vectors(:)

and

type(vector),pointer::pointed_vec

But when I compile, the compiler complains that:

This attribute specification is not valid for a component definition statement.

Can I have a pointer to a single item from an allocatable array? Is it possible?

A: 

Yes, you can point to a single item in an allocatable array, but your array my_vectors is a scalar, which cannot have the allocatable attribute. Maybe it's that declaration that's giving the compiler error. With gfortran I get a compile-time message:

  TYPE(vector),ALLOCATABLE,TARGET :: my_vectors
                                1
Error: Scalar object 'my_vectors' at (1) may not be ALLOCATABLE

Try declaring as my_vectors(:).

Deditos
Good spot, but that was just me failing to remember my code correctly. I do have an array declaration, not a scalar...
Phil H
Also I mis-read that your allocatable was also within a type definition. Unsure of large edit etiquette, so added new answer.
Deditos
+1  A: 

Only actual instances of variables or derived types may have the TARGET attribute. So, the allocatable in the second type definition cannot be a target as this is just a description of what the type should look like, a template if you like.

However, you can give a real instance of the type the TARGET attribute and then point to any of it's component parts with appropriately declared Fortran pointers.

Editted: An alternative, and probably more what you're after, is to give the vector array in the type the POINTER attribute only, which implicitly makes it both legitimate pointee and may be used to allocate memory. You just have to make sure that you don't reassign the pointer (v in example below) after you've used it to allocate the memory, because then you'll have a leak.

PROGRAM so_pointtype

  IMPLICIT NONE

  TYPE vec
    INTEGER :: x = 2, y = 3
  END TYPE vec

  TYPE foo
    TYPE(vec),POINTER :: v(:)
  END TYPE foo

  TYPE(foo) :: z
  TYPE(vec),DIMENSION(:),POINTER :: p2 => NULL()

  ALLOCATE(z%v(3))

  PRINT*,z%v(:)

  p2 => z%v(:)

  PRINT*,p2

END PROGRAM so_pointtype
Deditos
Makes sense, given the error, but that's quite frustrating! It means that what I do inside my type is dependent on whether I've declared something further up in the type hierarchy as target. Hmmm.
Phil H
Okay, I think the edited answer is what you're after.
Deditos