Next: Checkoff
Up: Multidimensional Arrays
Previous: Beyond One Dimension

Array Trickery

It is possible in Fortran to trick a function or subroutine into thinking that you have passed one kind of array when, in fact, you have passed a different kind. To do this kind of trickery, however, you must have a solid understanding of how arrays are laid out in the computer's memory. It's easy to get into trouble doing this, but the text makes such a big deal of it in section 14.2 that we've included this illustration.

Now take a look at ``marray3.f'' (or view it directly). How does it differ from ``marray2.f''?

Click here for the answer

Compile and run the program to convince yourself that it actually works. How in the world can it possibly work, when we are passing a 9x9x3 array to a subroutine that thinks it is getting a 9x9 array?

Recall that the subroutine is actually getting as an argument the address in memory of the 3-dimensional array. All arrays, no matter how many dimensions they have, are stored in consecutive memory locations. For example, a 2x2x2 array would be laid out as

(1,1,1) (2,1,1) (1,2,1) (2,2,1) (1,1,2) (2,1,2) (1,2,2) (2,2,2)

As you can see, the first subscript varies most rapidly. Now look at just the first four triples in the sequence above. What do you see?

Click here for the answer

If this doesn't make any sense to you, don't worry about it. Things rapidly get confusing when you pass around unmatched arrays in this fashion. But if it does make sense to you, be careful! Things rapidly get confusing when you pass around unmatched arrays in this fashion. If you run the ``Check file'' program on ``marray3.f'', you'll find that it generates a warning message.


Next: Checkoff
Up: Multidimensional Arrays
Previous: Beyond One Dimension

Hamlet Project
hamlet@cs.utah.edu