Pointers in Fortran

Contents
Point with =>
Associated test
Nullify
Prefer allocatables

Point with =>

A Fortran pointer is an alias: it refers to another variable instead of owning storage of its own.

Only a variable declared with the target attribute can be pointed at, and the link is made with the => operator.

The program below points p at val, so a change through p is visible in val.

program demo implicit none real, target :: val real, pointer :: p val = 10.0 p => val p = p + 5.0 print *, val end program demo

Associated test

The associated intrinsic reports whether a pointer currently points at something, which prevents accidental use of a dangling alias.

Check association before reading through a pointer that may still be empty, especially after optional setup steps.

This fragment prints one message when p is linked and another when it is not.

program demo implicit none real, target :: val real, pointer :: p nullify(p) if (associated(p)) print *, "linked" p => val if (associated(p)) print *, "linked" end program demo

Nullify

Nullify breaks the link between a pointer and its target without touching the target value itself.

A freshly declared pointer starts in an undefined state, so nullify it or point it at something before the first associated test.

The example links p, uses the target, and then safely disconnects the pointer.

program demo implicit none real, target :: val real, pointer :: p val = 3.0 p => val print *, p nullify(p) print *, associated(p) end program demo

Prefer allocatables

Allocatables are the safer default: they own their memory, clean up on their own, and cannot dangle the way pointers can.

Reach for a pointer only when aliasing is truly required, such as walking a linked structure or retargeting one name to several targets in turn.

The same storage below is managed with an allocatable and needs no nullify or association test at all.

program demo implicit none real, allocatable :: v(:) allocate(v(3)) v = [1.0, 2.0, 3.0] print *, v deallocate(v) end program demo

Next: DO CONCURRENT

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: