Search code examples
pointersfortranvariable-assignmentfortran90

What does => (equals greater than) mean in Fortran?


I'm looking through some old Fortran 90 code and have come across the => symbol:

var => item

It looks like it's being used for some sort of assignment.

Searching Google for "arrow symbol Fortran" or "equals greater than symbol Fortran" gives me no related material.


Solution

  • Surprisingly, searching "equals arrow symbol Fortran" yields some results.

    "=>" is commonly referred to as the pointer assignment operator.
    (Though it is not truly an operator as it returns no value.)

    It is used to associate a chosen reference name with a target:

    reference => target
    

    The above can be read as "reference refers to target"

    Most often this reference occurs in the form of a pointer. In this case you could say "reference points to target". This is a helpful way to remember what this operator does as its literal appearance is of an arrow pointing from reference to target.

    Further Uses
    Additional uses including making local aliases for various items such as module components, procedures, and even arbitrary expressions. For a full explanation of all of these uses see this answer.

    Pointer assignment vs. Traditional Assignment (=)
    Here's a quick example from the above link that illustrates how pointer assignment differs from classic assignment ("="). Basically this shows that once a target has been established, the pointer is treated as that target for basic statements.

    pt => x ! pt points to x
    
    y = pt ! y equals x
    
    pt => y ! pt points to y
    
    pt = 17 ! y equals 17
    

    Other Resources:
    General documentation on Fortran pointers