function interface question

From: Helge Avlesen (avle_at_tindved.ii.uib.no)
Date: 02/23/05


Date: Wed, 23 Feb 2005 13:29:56 +0100

Hi,
I have an array of a derived type object, where the object contains
pointers to different kinds of basic data types, e.g. arrays of
various dimensions. I would like to have a convenient interface to
fish pointers out of this list, so I tried to make a generic function,
but meet problems...

is it possible, in a "standard" way, to create a generic function from
functions with identical argument list, but different return type?
all compilers I have access to reject the below snippet because they
fail to see the difference between the functions (I guess). on the
other hand compilers seem happy if I return the pointer through the
argument list of a subroutine instead, but I do not really see why
this is easier to swallow for the compiler compared to checking the
return type of the function as below?

-- 
Helge
module arraylist
  type array_element
     real, pointer, dimension(:) :: ptr1d
     real, pointer, dimension(:,:) :: ptr2d
  end type array_element
  type hlist
     integer last
     type(array_element), pointer :: item(:) => null()
  end type hlist
  interface list_get
     module procedure get_item1d, get_item2d
  end interface
  
contains
  
  function get_item1d(list,wanted_n)
    implicit none
    real, pointer, dimension(:) :: get_item1d
    type(hlist) :: list
    integer, optional :: wanted_n
    integer n
    n=list%last
    if(present(wanted_n))n=wanted_n    
    get_item1d => list%item(n)%ptr1d
  end function get_item1d  
  function get_item2d(list,wanted_n)
    implicit none
    real, pointer, dimension(:,:) :: get_item2d
    type(hlist) :: list
    integer, optional :: wanted_n
    integer n
    n=list%last
    if(present(wanted_n))n=wanted_n    
    get_item2d => list%item(n)%ptr2d
  end function get_item2d
  
end module arraylist


Relevant Pages

  • Re: Array of pointer Vs Pointer to Array
    ... that points to an array. ... Now 'pa' is a pointer form of accessing the values in 'matrix'. ... that depends on a variety of things - the size of the other dimension, ... the regular indexing style is a good first guess, ...
    (comp.lang.c)
  • Re: [links] [C] 2-dimensional arrays and functions
    ... > But isn't an array just a pointer to its first index anyway? ... > int main ... an array of of dimension dim1, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Pointers to pointers
    ... a pointer help a design or aglorithm? ... A two dimensional array is an array of arrays. ... unless the second dimension is fixed. ... pointer to the first element and do the indexing calculations yourself ...
    (comp.lang.c)
  • fortran pointer structure
    ... and fortran subroutine. ... argument (where it is defined as a pointer). ... direction (allocation in Fortran and write in the allocated space in C++). ... total nunber of elements (1st dimension size)* ...
    (comp.lang.fortran)
  • Re: zero-based array, in function confucsion
    ... and it shows that only the 1st dimension of an ... > array of arrays decays to a pointer upon passing to a fn. ...
    (comp.lang.c)