Reputation: 433
I would like to create a pointer to an array with smaller dimension.
For example, I have some array arr(1:2, 1:10, 1:10)
.
Now I want to create a pointer to arr(1:1, 1:10, 1:10)
but I want to delete first I don't know how I should name it by it look like index, and second pointer to (2:2, 1:10, 1:10).
I need it because I would like to send array with 2 dimensions (matrix) to a function.
Here is an indication of what I want to do, with pseudocode.
INTEGER, DIMENSION (1:2, 1:10, 1:10), TARGET :: BOUNDRIES
INTEGER, DIMENSION (:,:), POINTER : LEFT_BOUNDRY
LEFT_BOUNDRY => BOUNDRIES(1,1:10,1:10)
DO i = 1,n
DO j = 1,10
write(*,*) LEFT_BOUNDRY(i,j)
END DO
END DO
Is it possible to do it?
Upvotes: 1
Views: 642
Reputation: 32366
When we have a dummy argument in a function or a subroutine (collectively, procedure) we have a corresponding actual argument when we execute that procedure. Consider the subroutine s
:
subroutine s(x)
real x(5,2)
...
end subroutine s
The dummy argument x
is in this case an explicit shape array, of rank 2, shape [5,2]
.
If we want to
call s(y)
where y
is some real
thing we don't need to have y
a whole array which is of rank 2 and shape [5,2]
. We simply need to have y
have at least ten elements and a thing called storage association maps those ten elements to x
when we are in the subroutine.
Imagine, then
real y1(10), y2(1,10), y3(29)
call s(y1)
call s(y2)
call s(y3)
Each of these works (in the final case, it's just the first ten elements that become associated with the dummy argument).
Crucially, it's a so-called element sequence that is important when choosing the elements to associate with x
. Consider
real y(5,12,10,10)
call s (y(1,1,1:2,3:7))
This is an array section of y of ten elements. Those ten elements together become x
in the subroutine s
.
To conclude, if you want to pass arr(2,1:10,1:10)
(which is actually a rank 2 array section) to a rank 2 argument which is an explicit shape array of no more than 100 elements, everything is fine.
Upvotes: 2