ATK
ATK

Reputation: 1526

Reshaping 1d array to 2D pointer array Fortran

I have a pointer uvw(:,:) which is two-dimensional, and I got a 1d buffer array x(:).

Now I need to point uvw(1,:)=>x(1:ncell) and uvw(2,:)=>x(ncell+1:ncell*2) etc.

I made a very simple example. I know that array of pointers does not work, but does anybody have an idea how this can be worked around?

PS: For a pragmatic reason I do not want to wrap my uvw with a declared type. ( i am changing some bit of code, and need uvw as 2D pointer. Currently is an array, and my idea is to avoid changing the way uvw is being used as it being used thousands of times)

 program test

 real, allocatable,target :: x(:)
 real, pointer :: ptr(:,:)


allocate(x(100) )


x = 1.

ptr(1,:) => x(1:10)

end program

The error message says:

`error #8524: The syntax of this data pointer assignment is incorrect: either 'bound spec' or 'bound remapping' is expected in this context. [1]

ptr(1,:) => x(1:10)

----^`

Upvotes: 0

Views: 1140

Answers (2)

Holmz
Holmz

Reputation: 724

If you prefer avoiding a pointer, then the UNION/MAP is an option depending on compiler. It was added to gfortran a while ago... then you can think of the array as a rank=2 but also use the vector (Rank=1) for SIMD operations.

All this assumes that one wants to avoid pointers...

Upvotes: -1

francescalus
francescalus

Reputation: 32366

You are trying to perform pointer bounds remapping, but you have the incorrect syntax and approach.

Pointer bounds remapping is a way to have the shape of the pointer different from that of the target. In particular, the rank of the pointer and target may differ. However, in such an assignment it is necessary to explicitly specify the lower and upper bounds of the remapping; it isn't sufficient to use : by itself.

Also, you'll need to assign the whole pointer in one go. That is, you can't have "the first ten elements point to this slice, the next ten to this slice" and so on in multiple statements.

The assignment statement would be

ptr(1:10,1:10) => x

Note, that this also means that you can't actually have what you want. You are asking for the elements ptr(1,1:10) to correspond to x(1:10) and ptr(2,2:10) to correspond to x(11:20). That isn't possible: the array elements must match in order: ptr(1:10,1) being the first ten elements of ptr must instead be associated with the first ten elements x(1:10). The corrected pointer assignment above has this.

Upvotes: 2

Related Questions