Re: initializing an array of user-defined data types



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
.



Relevant Pages

  • Re: Function lookup tables?
    ... Ignore any typos the source is good except howto initialize the function ... > I want to initialize the function lookup table array to my member ... > function calls of the class inside of the constructor for the class. ... > What is the correct code to initialize the function pointer array ...
    (comp.lang.cpp)
  • Re: pointer plus an integer
    ... allocating a double matrix with the dmatrix ... The dmatrix() function is appended below. ... an array of fixed maximum size has to be created at compile time. ... Ais a pointer to a single array and the statement ...
    (comp.lang.c)
  • Re: Question about arrays of derived types and POINTERs
    ... initialize surfinstead of allocating a temporary surfptr and then ... a pointer starts "life" with an undefined association ... nulifying the x, y, and z pointers in each element when you initialize ...
    (comp.lang.fortran)
  • Re: Access to data into an array causes seg-fault
    ... >> How are you returning the array to main? ... > well, in main, i declare it as double **sound, then call my ... you must pass a pointer to that parameter. ... pointer value in your allocating function and have that change ...
    (comp.lang.c)
  • Re: Array vs Vector
    ... to the array and performing arithmetic on that pointer. ... limitation is allocating the array dynamically: ... resizing and other functionality through methods. ...
    (comp.lang.c)