Re: initializing an array of user-defined data types



On Nov 28, 1:12 pm, nos...@xxxxxxxxxxxxx (Richard Maine) wrote:
Bart Vandewoestyne <MyFirstName.MyLastN...@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- Hide quoted text -

- Show quoted text -

In CVF, NULL is simply an integer variable of value zero imported from
one of the windows support modules. Used alot for c interperability.
.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: Copying an array slice (Was: Re: Difficulties with passing multi-dimensional arrays)
    ... > the pointer to array of unknown size. ... but it still compiles without a cast. ... unknown at compile time so nothing can be checked. ...
    (comp.lang.c)