From: zkdc05@trc.amoco.com (Kelly D. Crawford)
Subject: Re: What are pointers
Originator: zkdc05@church
Organization: Amoco Production Company, Tulsa Research
Date: Tue, 6 Oct 1992 14:19:17 GMT


In article <3OCT199222373032@rosie.uh.edu>, math1rb@rosie.uh.edu (92F07486) writes:
> For one of my classes, we need to manipulate some matrices and the prof.
> said to use pointers. What are pointers?

Now that it has been well debated as to exactly what prompted this posting, I think
I will try to answer this person's question!

Pointers are variables that hold "address" information, where an address is simply
a location in memory.  These addresses can either be relative or absolute.  An
example of a relative address would be an array index in a simple Fortran array.
Examples of absolute addresses in Fortran (yes, there actually is a *simple* form
of these in standard Fortran) would be passing arguments to subroutines and
functions (which is done by address), and external functions (which can be passed
to subroutines and functions).  By passing variables and arrays by address, the
subroutine or function called can actually work on the exact same data that the
caller was using.

There is also a non-standard pointer declaration included in many Fortran 77
compilers, and pointers are standard under Fortran 90.

For matrix manipulation, it is very common to pass in only the portion of the
array you want to use to a subroutine.  This is done by passing in (what
appears to be) the first element of the array section you need.  But since
Fortran passes arguments by address, you have actually given the subroutine
a "pointer" to the data.  The following example shows how you would pass in
each column of a matrix, one at a time, to a subroutine called norm (that might
calculate the 2-norm of a vector, for example):

      program matrix
      integer m, n, i
      parameter (m = 4, n = 5)
      integer A(m,n)
      ...
      do 10 i = 1, n
* The first column starts at A(1,1), the second at A(1,2), ..., the n'th at A(1,n)
* These array locations can be considered as "pointers" since they in effect
* point the subroutine to the desired memory locations.
         call norm(A(1,i),m)
 10   continue
      ...
      end
      subroutine norm(v, m)
      integer m, v(m)
* The i'th column of matrix A will be treated as vector v of length m
      ...
      end

There are all sorts of reshaping tricks that you can play with matrices as
subroutine arguments.  You can find many useful and documented examples
in Linpack and Lapack.

I know this was terse, but I hope it helps.  Don't feel bad if you end up having
trouble with pointers.  It is a major stumbling block for many people and
considered by some to be one of the most difficult hurdles of computer
science (which is odd, because the concept is actually simple...once grasped!)

Kelly
-----
-- 
Kelly D. Crawford             | Amoco Production Research | These are my own
kcrawford@trc.amoco.com       | 4502 East 41st Street     | opinions, not
phone: (918) 660-4043         | Tulsa, OK  74102          | Amoco's...
          >>> *** This line intentionally left blank *** <<<
Xref: sparky comp.lang.fortran:3844 comp.std.misc:127