Pointers

Pointer Variables

•     all variables seen so far (except allocatable arrays) are statically defined

•     every variable name corresponds to a fixed set of memory locations

•     this correspondence cannot be changed during program execution

•     cannot create and destroy variables as and when required

•     pointer variables allow this (and more)

Pointer Variables

•     a pointer variable does not store a data value

•     instead, the address of a location containing the data is stored in a pointer variable

•     pointer is said to “point” to the location (variable) whose address it contains

•     variable pointed to is called the “target”

•     targets of a pointer may be changed during program execution

•     type of pointer defined by the type of targets

Pointer Variables

•     a pointer variable pointing to a target is an alias for the target and is said to be associated with the target

•     target can be accessed using the pointer rather than the name of target variable

•     same data can be accessed by different names

•     changing targets allows using same name for different variables (of same type)

Declarations

•     variables  given pointer or target attribute

integer, pointer :: ptr

integer, target :: i

•     ptr is an integer pointer which can point to targets of integer type

•     i is an integer variable which can be the target of an integer pointer

ptr => i ! (sets variable i as target of ptr)

! address of variable i stored in ptr

Pointer Assignment

•     when declared, a pointer variable is undefined

•     it can be assigned a special “null” value using an intrinsic function nullify(ptr)

•     in F95 this can be done as ptr = null()

•     null pointer not associated with any target

•     associated with a target using pointer assignment

pointer => target

•     associated(ptr,target) is an intrinsic function, true if ptr is associated with target (optional)

Using Pointers

•     a pointer associated with a target is an alias for the target variable

•     it can be used like any other variable of target type

•     value assigned to pointer actually gets assigned to the target variable (called de-referencing)

•     pointers are automatically de-referenced (refer to target variable) except when used in pointer assignments, or passed as pointer arguments

Example

integer, pointer :: p, q

integer, target :: i, j

p => i

! pointer p associated with target i

p = 5 ! target of p assigned value 5, same as i = 5

q => j ! target of q is variable j

q = p  ! same as j = i, j gets value 5

q => p ! target of q is now target of p, that is i

! pointers of same type can always be targets

! p, q and i all refer to the same variable now

Allocating Pointers

•     target of pointer need not be a declared variable

•     can create a “new” variable of  target type

•     space for new variable allocated at run time

•     variable can only be accessed through pointer

•     pointer variable name used for it

•     if pointer is nullified, variable is inaccessible unless other pointers also point to it

•     pointer can be deallocated when target is not required (no other pointers should have same target)

Allocating Pointers

integer, pointer :: ptr

integer, target :: i

allocate (ptr, stat=i)

! creates a new integer variable as target of ptr

! this new variable does not have any other name

! stat should be used to check success of allocation

ptr = i

! new variable assigned value of i

print *, ptr

deallocate (ptr, stat = i)

 

Array Pointers

•     the target of a pointer may be an array

•     only the rank and type of array is fixed

•     targets may have different shapes

integer, dimension(:), pointer :: ptr

! ptr is a pointer to a rank 1 integer array

integer, dimension(100), target :: a

ptr => a            ! ptr is now associated with array a

print *,  ptr(1:n) ! ptr can now be used instead of a

allocate(ptr(20), stat=i) ! ptr is now associated with a

! new array of size 20

! similar to allocatable arrays but more flexible

Pointer Arguments

•     pointer variables may be passed as arguments to subprograms

•     corresponding dummy argument may or may not have pointer attribute (must have same type)

•     if dummy argument is not a pointer, target of actual argument is passed else pointer is passed

•     unassociated pointer can be passed as argument and association can be done in subprogram

•     pointer dummy arguments cannot have intent attribute

Pointer Arguments

integer, pointer :: ptr ; integer, target :: i

ptr => i

call what_1(ptr)

print *, ptr

call what_2(ptr)

print *, ptr

subroutine what_1(p)

integer, intent(inout) :: p

……

subroutine what_2(ptr)

integer, pointer :: ptr