Goeran Klose writes:
[with appropriate derived type definitions]
> Do the following two Fortran lines mean the same ?
>
> 1: c%big(:,:,:)%a = b%big(:,:,:)%a
> 2: c%big%a = b%big%a
Yes...with 2 caveats.
1. Only one "part" of of a data-ref can be an array. (The parts are
the things separated by the % characters). In your case, this
is ok. The part that is an array is the middle one (big or
big(:,:,:)).
If b and c were arrays, then you couldn't do this in either form,
because the first and second parts would both be arrays. That is,
you could never write c(:)%big(:,:,:)%a and you can't do it as
c%big%a in that case either. You'd need something like
c(i)%big%a or c(i)%big(:,:,:)%a, where you looped through the i
values.
2. Completely separate of all the derived type stuff, for any array x,
x(:), is not quite the same thing as x. The x(:) is an array
slice, the x is not. In many contexts, including your simple
assignment, this doesn't matter. But in other contexts it does.
If you incorrectly think of them as 2 ways of writing the same
identical thing, you will eventually get into trouble. In
particular
a. x may be subscripted, whereas x(:) may not. That is, you can
write x(3), but you can't write x(:)(3). Some people may find this
obvious. Others may not (and may even ask why - but that's
been discussed here before).
b. x may have a lower bound different from 1. But the lower bound
of an array expression like x(:) is always 1. There are
contexts where that matters. (Though you do have to go to a
little work to come up with such contexts, as you can't directly
subscript x(:).)
P.S. On what seems to be the biggest thread at the moment, even though
it's not actually on matters Fortran.... I send the large majority of
replies (like this one) to the list, and greatly prefer that to be the
default. Just like I normally prefer that replies to my postings be
send to the list. If someone really wants to meet me for dinner at
the next J3 meeting, they can send me email. But if they are making a
point about almost anything Fortran, the majority of such things
belong on the list. Exceptions exist, but they are rare. I get far
more email replies that I think should not have come to me
individually than ones I think appropriate. (I do not run a free
personal Fortran consulting service, although I often volunteer
replies to things posted on this list or comp.lang.fortran.) If I
want to reply to the poster, then I can always do something like copy
from their signature. It is always a good idea to include an email
address in a signature. Then it's always available, no matter how
headers might get mangled. (Of course, if you don't want people to be
able to contact you easily, for spam protection or other reasons,
that's a separate question).
--
Richard Maine
[log in to unmask]
|