Pointers


Originally you said you can only use a pointer in an integer variable statement, but later you wrote a pointer in a real statement. Can you do both?

Sorry for the confusion. I told you that a pointer is an integer, which is true internally. At its root, a pointer contains an integer giving the address in memory of some variable. However, the pointer variable carries with it a type definition telling what type variables it can point to. In Fortran you never see the underlying memory address stored with the pointer. The pointer can have any data type, including real, integer, logical, character, complex, and derived types. It can only point to targets with the same data type.

Target is the original variable and pointer is the variable that is set equivalent to the target (e.g. px=>x) right?

Yes.

Why can't you assign a pointer to an individual space in an array?

You can if the pointer is not an array itself. For example

      real, target :: a(100)
      real, pointer :: aptr
      aptr => a(50)
will work just fine. The pointer variable "aptr" is equivalent to element 50 of array "a".

Can pointers be placed anywhere in a program?

Pointer assignments ( pa => a) are "executable" statements and can be placed anywhere that other executable statements may be placed, providing proper target and pointer declarations have been made in the preceding non-executable statement block. Once assigned, the pointer variable can be used anywhere that a normal variable would be used in executable statements. Three places that pointer variables can never be used are DATA, NAMELIST (we skipped that one), and EQUIVALENCE statements.

What are some more functions of pointers? I'm a little unclear as to what they can do and what they are used for.

I'm at a loss to give you a clean example in science and engineering, without giving you a couple semesters worth of applications involving solution of partial differential equations. It boils down to a question of how you have to arrange data for modeling complex physical systems. I've talked frequently about simulating the flow of air in our classroom, but what if I wanted a program that could model the flow of air in the whole building, in fact in any building. The grid of points that I would lay down to evaluate air properties at points in the building would be very complex, and in a good program, generated at the time input specified the geometry of the building. In practice, solving the problem for the entire building is best accomplished by looking at the solution in each room and then accounting for air flows between the rooms. You have a full set of the grid points for the building, with subsets representing individual rooms. Pointers do a nice job of picking up subsets of points for each room to do the local solution steps.

If you want a computer science example, take a look at section 20.7 p. 537 in your textbook, where the author works with a linked list. Linked lists are very important in the computer science world, but have limited application when creating models of physical systems.

Are pointers used in C++? If so, do they function the same as in C or Fortran?

Standard C is a contained in C++ just as Fortran 77 is contained in Fortran 90, so you automatically have C pointers. C++ adds a data type called the "Reference Type", that is like a Fortran pointer.


Up to other Questions and Answers / Home


Maintained by John Mahaffy : jhm@psu.edu