Re: initializing an array of user-defined data types
- From: nospam@xxxxxxxxxxxxx (Richard Maine)
- Date: Wed, 28 Nov 2007 11:12:41 -0800
Bart Vandewoestyne <MyFirstName.MyLastName@xxxxxxxxxx> wrote:
I have a rank 2 array of a user-defined type as follows:
type(piece), dimension(:,:), allocatable, private :: board
After allocating it, I want to initialize it so that after
initialization, I can check whether board(i,j) contains a piece
or not.
I've tried things like
board = NULL
... assign some pieces to board positions and leave others empty ...
if (board(i,j) == NULL) then
... do something ...
end if
What is this NULL thing you seem to expect? There is no such thing. The
only thing named NULL in Fortran is an intrinsic function that returns a
pointer or allocatable. Perhaps you are thinking that applies here
because your array is allocatable. That isn't so. The array is
allocatable, but the individual elements are not. You also don't have
the right syntax for NULL, as it is a function, but fixing the syntax
won't fit because the function isn't applicable here.
The way that you want to designate an empty spot is basically up to you.
You haven't shown anythiong about what your type piece would look like,
so I can't comment in any detail. But just establish some convention of
your own and then use it. There isn't any particular language feature
that will magically help with this. There is no concept of array
locations being absent.
For a trivial example, the derived type could have a logical component
with a value indicatinmg whether the spot was occupied. Or if there is
some code for what kind of piece is there, you could assign a code value
for there being no piece.
You could even use NULL() by having a component of piece be a pointer,
which you could then nullify, but it would be a component - not the
whole element. And I wouldn't do it that way unless there was some other
use for the pointer. One can use a pointer association status as a
long-winded way of encoding a true/false value, but a logical variable
is a much more... um... logical encoding if that's all there is to it.
As an aside, the organization sounds... either unusual or confusingly
named. I don't normally think of a board as being an array of pieces,
but rather as an array of positions, each of which could have a piece on
it. I suppose that one could consider it as an array of pieces (provided
you have an appropriate null piece), but then you wwould not have pieces
move, but rather each "piece" would always stay in the same location on
the board, but would change its identity. That kind of organization can
work, but for that kind of organization I'd tend to call the elements
something more like a locations or cells instead of a pieces.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.
- Follow-Ups:
- Re: initializing an array of user-defined data types
- From: glen herrmannsfeldt
- Re: initializing an array of user-defined data types
- From: garylscott
- Re: initializing an array of user-defined data types
- References:
- initializing an array of user-defined data types
- From: Bart Vandewoestyne
- initializing an array of user-defined data types
- Prev by Date: Re: Pointers and DLLs/shared libraries
- Next by Date: Re: initializing an array of user-defined data types
- Previous by thread: initializing an array of user-defined data types
- Next by thread: Re: initializing an array of user-defined data types
- Index(es):
Relevant Pages
|