Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28455 Discussions

Pointing to an array slice with negative indices

David_DiLaura1
574 Views

Colleagues,

 

I have (very) distant memory of encountering this problem before. Cannot recall the solution.

 

P is declared a pointer    P(: ,

D is target and dimensioned:  D(-180:180, -180:180)

F is target and dimensioned: F(-180:180, -180:180,10)

 

Assigning P to D

P => D    P has negative indices and works in subsequent code expecting (-180:180, -180:180)

 

Assigning P to  a page of F

P => F(: , : , 3)   P does not have indices: (1:361, 1:361). Using P in subsequent code fails.

 

Is there a way to point to a slice of an array with negative indices?

 

 

0 Kudos
1 Solution
FortranFan
Honored Contributor II
558 Views

@David_DiLaura1 ,

May be if you try this minimal working example and modify it to illustrate the problem, it will be easier for readers to provide some guidance:

   integer, target :: F(-2:2,-2:2,2)
   integer, pointer :: P(:,:)
   F = reshape( source=[( i, integer :: i = 1, size(F) )], shape=shape(F) ) 
   P => F(:,:,2)
   print *, "shape(P) = ", shape(P), "expected is [5,5] "
   print *, "P(2,4) = ", P(2,4), "expected is ", F(-1,1,2)
end
C:\temp>ifort /standard-semantics t.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.6.0 Build 20220226_000000
Copyright (C) 1985-2022 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.30.30706.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:t.exe
-subsystem:console
t.obj

C:\temp>t.exe
 shape(P) =  5 5 expected is [5,5]
 P(2,4) =  42 expected is  42

View solution in original post

0 Kudos
2 Replies
FortranFan
Honored Contributor II
559 Views

@David_DiLaura1 ,

May be if you try this minimal working example and modify it to illustrate the problem, it will be easier for readers to provide some guidance:

   integer, target :: F(-2:2,-2:2,2)
   integer, pointer :: P(:,:)
   F = reshape( source=[( i, integer :: i = 1, size(F) )], shape=shape(F) ) 
   P => F(:,:,2)
   print *, "shape(P) = ", shape(P), "expected is [5,5] "
   print *, "P(2,4) = ", P(2,4), "expected is ", F(-1,1,2)
end
C:\temp>ifort /standard-semantics t.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.6.0 Build 20220226_000000
Copyright (C) 1985-2022 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.30.30706.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:t.exe
-subsystem:console
t.obj

C:\temp>t.exe
 shape(P) =  5 5 expected is [5,5]
 P(2,4) =  42 expected is  42
0 Kudos
FortranFan
Honored Contributor II
556 Views

Also for general Fortran language related discussion, you may find the Fortran Discourse site also helpful:

https://fortran-lang.discourse.group/

0 Kudos
Reply