keinert@IASTATE.EDU (Keinert Fritz) (05/17/91)
Thanks to all who replied to my question about pointers in Fortran. Our Internet connection was down for a couple of days, so e-mail was delayed, and this newsgroup has not been downloaded yet. I hope this posting makes it out, and does not duplicate too many other postings. According to the replies I got, pointers are documented in the Fortran for RISC Installation Guide, rather than the manual, and also in Sun, SGI and Cray Fortran. I found them in a Cray manual. I thought the declaration for a pointer to real was pointer (p,real) This is incorrect, or rather, it defines p as a pointer to a variable called "real". This is not what I intended. The correct notation is real r pointer (p,r) which defines a variable p of type "pointer to real". As far as calculating and printing goes, p is treated as an integer. There is no storage reserved for the variable r, even though it appears that way. Rather, r is the name that is given to the contents of p. In C notation, r would be called *p. There is a referencing operator loc(), but no dereferencing operator. Instead, the dereferenced version of a pointer has its own name. A fancy way to copy the value of y to x, in C: float x, y, *p; y = 3.; p = &y; x = *p; The same in Fortran: real x, y, r pointer (p, r) y = 3. p = loc(y) x = r This way of handling pointers is really clumsy. In C: float x[5], *p; for (p = x; p < x+5; p++) *p = 0.; becomes in Fortran real x(5), r pointer (p,r) p = loc(x(1)) do i = 1, 5 r = 0. p = p + 4 enddo The only good use I found for pointers in Fortran at the moment is dynamic memory allocation (see separate posting). -- Fritz Keinert phone: (515) 294-5128 Department of Mathematics fax: (515) 294-5454 Iowa State University e-mail: keinert@iastate.edu Ames, IA 50011