Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays of pointers

I am trying to implement an array of pointers, so that I can loop over the elements. However I am not sure how to do this correctly:

TYPE(domain),POINTER              :: d01,d02,d03 TYPE(domain),DIMENSION(:),POINTER :: dom ...  dom(1) => d01 dom(2) => d02 dom(3) => d03 ... 

and then:

... IF(ASSOCIATED(dom(2),d02))THEN ... 

The compiler (pgf90 10.6-0 64-bit target on x86-64 Linux -tp istanbul-64) gives me this error message:

 PGF90-S-0074-Illegal number or type of arguments to associated - keyword argument pointer (test_ptr.f90: 10)   0 inform,   0 warnings,   1 severes, 0 fatal for MAIN 

As far as I understand, there is something wrong about how I subset an array of pointers. Both dom(2) and d02 are rank 0 (scalar pointers). What is the correct way to implement this?

Thanks.

like image 813
milancurcic Avatar asked Jan 17 '12 19:01

milancurcic


People also ask

What is array of pointers in C?

Declaration of an Array of Pointers in C In simple words, this array is capable of holding the addresses a total of 55 integer variables. Think of it like this- the ary[0] will hold the address of one integer variable, then the ary[1] will hold the address of the other integer variable, and so on.

What is array of pointers Class 12?

The one dimensional or two-dimensional pointer array is called array of pointer. For example, int *ptr[5];

What are pointers & arrays?

Array in C is used to store elements of same types whereas Pointers are address varibles which stores the address of a variable. Now array variable is also having a address which can be pointed by a pointer and array can be navigated using pointer.

Can pointers be used as arrays?

In simple words, array names are converted to pointers. That's the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same. There are a few cases where array names don't decay to pointers.


1 Answers

Yeah, pointer arrays are funny in Fortran.

The problem is that this:

TYPE(domain),DIMENSION(:),POINTER :: dom 

does not define an array of pointers, as you might think, but a pointer to an array. There's a number of cool things you can do with these things in Fortran - pointing to slices of large arrays, even with strides - but it is definitely a pointer to an array, not an array of pointers.

The only way to get arrays of pointers in Fortran is to define a type:

type domainptr   type(domain), pointer :: p end type domainptr  type(domainptr), dimension(3) :: dom  dom(1)%p => d01 dom(2)%p => d02 dom(3)%p => d03 

etc. As far as I can tell, the only real reason you have to do this in Fortran is syntax. I'd love to see this fixed in some later version of the standard.

like image 77
Jonathan Dursi Avatar answered Sep 20 '22 22:09

Jonathan Dursi